- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a data frame in R with repeated rows by a sequence of number of times or by a fixed number of times?
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
- Related Articles
- How to create an empty data frame with fixed number of rows and without columns in R?
- How to divide data frame rows by number of columns in R?
- How to create a vector of data frame values by rows in R?
- How to repeat a simulation to a fixed number of times in R?
- How to randomly select a fixed number of rows without replacement from a data frame in R?
- How to count the number of times a value occurs in a column of an R data frame?
- How to remove rows in an R data frame column that has duplicate values greater than or equal to a certain number of times?
- How to repeat a column of a data frame and join it with another data frame in R by rows?
- How to increase the length of an R data frame by repeating the number of rows?
- How to return a string repeated N number of times in C#?
- How to convert a vector to data frame in R by defining number of columns?
- How to find the number of times a variable changes its sign in an R data frame column?
- How to create a data frame with a column having repeated values in R?
- How to create combinations of 0 and 1 with fixed number of 1s in each row in an R data frame?
- How to filter rows by excluding a particular value in columns of the R data frame?
