
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Data Frame in R with Repeated Rows
There are times when duplicated rows in a data frame are required, mainly they are used to extend the data size instead of collecting the raw data. This saves our time but surely it will have some biasedness, which is not recommended. Even though it is not recommended but sometimes it becomes necessary, for example, if it is impossible to collect raw data then we can do it. If we do so then we must specify it in our analysis report. In R, we can use rep function with seq_len and nrows to create a data frame with repeated rows.
Example
Consider the below data frame df −
> x<-1:10 > y<-letters[1:10] > df<-data.frame(x,y)
Creating a new data frame in which the rows are printed one more after original rows −
> df[rep(seq_len(nrow(df)), times = 2), ] x y 1 1 a 2 2 b 3 3 c 4 4 d 5 5 e 6 6 f 7 7 g 8 8 h 9 9 i 10 10 j 1.1 1 a 2.1 2 b 3.1 3 c 4.1 4 d 5.1 5 e 6.1 6 f 7.1 7 g 8.1 8 h 9. 1 9 i 10.1 10 j
Creating a new data frame in which the duplicate rows are printed one by one −
> df[rep(seq_len(nrow(df)), each = 2), ] x y 1 1 a 1.1 1 a 2 2 b 2.1 2 b 3 3 c 3.1 3 c 4 4 d 4.1 4 d 5 5 e 5.1 5 e 6 6 f 6.1 6 f 7 7 g 7.1 7 g 8 8 h 8.1 8 h 9 9 i 9.1 9 i 10 10 j 10.1 10 j
Repeating each row by sequence of numbers −
> df[rep(seq_len(nrow(df)), times = 1:10), ] x y 1 1 a 2 2 b 2.1 2 b 3 3 c 3.1 3 c 3.2 3 c 4 4 d 4.1 4 d 4.2 4 d 4.3 4 d 5 5 e 5.1 5 e 5.2 5 e 5.3 5 e 5.4 5 e 6 6 f 6.1 6 f 6.2 6 f 6.3 6 f 6.4 6 f 6.5 6 f 7 7 g 7.1 7 g 7.2 7 g 7.3 7 g 7.4 7 g 7.5 7 g 7.6 7 g 8 8 h 8.1 8 h 8.2 8 h 8.3 8 h 8.4 8 h 8.5 8 h 8.6 8 h 8.7 8 h 9 9 i 9.1 9 i 9.2 9 i 9.3 9 i 9.4 9 i 9.5 9 i 9.6 9 i 9.7 9 i 9.8 9 i 10 10 j 10.1 10 j 10.2 10 j 10.3 10 j 10.4 10 j 10.5 10 j 10.6 10 j 10.7 10 j 10.8 10 j 10.9 10 j