Repeat Rows of DataFrame N Times in R
Last Updated :
14 Sep, 2021
In this article, we will discuss how to repeat rows of Dataframe for a given number of times using R programming language.
Method 1 : Using replicate() method
A replication factor is declared to define the number of times the data frame rows are to be repeated. The do.call() method in R is used to perform an R function while taking as input various arguments. The rbind() method is taken as the first argument of this method to combine data frames together. The second argument is the replicate() method which is used to create multiple copies of the rows of the data frames equivalent to the number of times same as replication factor.
Syntax: replicate(n, expr, simplify)
Parameter :
- n - Number of replications to be performed.
- expr - Expression to be performed repeatedly.
- simplify - Type of output the results of expr are saved into.
Example: Repeating rows n times
R
# creating a data frame
data_frame <- data.frame(col1 = c(6:8),
col2 = letters[1:3],
col3 = c(1,4,NA))
print ("Original DataFrame")
print (data_frame)
# replication factor
n <- 3
data_frame_mod <- do.call("rbind", replicate(
n, data_frame, simplify = FALSE))
print ("Modified DataFrame")
print(data_frame_mod)
Output
[1] "Original DataFrame"
col1 col2 col3
1 6 a 1
2 7 b 4
3 8 c NA
[1] "Modified DataFrame"
col1 col2 col3
1 6 a 1
2 7 b 4
3 8 c NA
4 6 a 1
5 7 b 4
6 8 c NA
7 6 a 1
8 7 b 4
9 8 c NA
Method 2: Using purrr package
The purr package in R is used to simplify the functioning and working with both functions as well as vectors. The package can be installed into the working space using the following command :
install.packages("purrr")
The seq_len() method in R is used to create a sequence taking as input an integer value and then generating a sequence of numbers beginning from 1 to the specified integer with steps equivalent to 1.
Syntax:
vec <- seq_len(number)
The map_dfr() function of the purrr in R is used to create a data frame formed by row appending. It is used for row binding.
Syntax:
map_dfr(vec, ~data-frame)
Example: Repeating rows n times
R
library("purrr")
# creating a data frame
data_frame <- data.frame(col1 = c(6:8),
col2 = letters[1:3],
col3 = c(1,4,NA))
print ("Original DataFrame")
print (data_frame)
# replication factor
n <- 2
data_frame_mod <- purrr::map_dfr(seq_len(n), ~data_frame)
print ("Modified DataFrame")
print(data_frame_mod)
Output
[1] "Original DataFrame"
col1 col2 col3
1 6 a 1
2 7 b 4
3 8 c NA
[1] "Modified DataFrame"
col1 col2 col3
1 6 a 1
2 7 b 4
3 8 c NA
4 6 a 1
5 7 b 4
6 8 c NA
Method 3: Using rep() method
The nrow() method in R is used to determine the number of rows of the data frame.
Syntax:
nrow(data-frame)
The vector sequence is then generated using the seq_len() method described in the previous method. This is followed by the application of rep() method which is used to replicate numeric values a specified number of times. The first argument is the vector generated by seq_len() method and n is the replication factor.
Syntax:
rep(vec, n)
Data frame indexing is used to append the generated rows to the original data frame rows and stored as the modified data frame.
Example: Repeating rows n times
R
# creating a data frame
data_frame <- data.frame(col1 = c(6:8),
col2 = letters[1:3],
col3 = c(1,4,NA))
print ("Original DataFrame")
print (data_frame)
# replication factor
n <- 3
data_frame_mod <- data_frame[rep(seq_len(nrow(data_frame)), n), ]
print ("Modified DataFrame")
print(data_frame_mod)
Output
[1] "Original DataFrame"
col1 col2 col3
1 6 a 1
2 7 b 4
3 8 c NA
[1] "Modified DataFrame"
col1 col2 col3
1 6 a 1
2 7 b 4
3 8 c NA
1.1 6 a 1
2.1 7 b 4
3.1 8 c NA
1.2 6 a 1
2.2 7 b 4
3.2 8 c NA
Similar Reads
Repeat Character String N Times in R In this article, we will discuss how to repeat the character string N times in the R programming language. Character string means a set of characters . Example: "Hello Geek","Python","Languages_Python" are some examples Method 1: Using replicate() method This function used to give n replicas from th
3 min read
Replace Inf with NA in Dataframe in R In this article, we will discuss how to replace Inf (Infinity) with NA in Dataframe in R Programming Language. Create a dataframe with for demonstration: R # create a dataframe with 3 columns and 5 rows data=data.frame(column1 = c(Inf, 2, 3, 4, 5), column2 = c(1, Inf, 1, Inf, 1), column3 = c(1,2,3,I
2 min read
Find the unique rows in R DataFrame In an R data frame, a unique row means that none of the elements in that row are replicated in the whole data frame with the same combination. In simple terms, if we have a data frame called df with four columns and five rows, we can assume that none of the values in one row are replicated in every
1 min read
How to Remove Duplicate Rows in R DataFrame? In this article, we will discuss how to remove duplicate rows in dataframe in R programming language. Dataset in use:Method 1: Using distinct() This method is available in dplyr package which is used to get the unique rows from the dataframe. We can remove rows from the entire which are duplicates a
2 min read
How to change Row Names of DataFrame in R ? The rows are stacked together, each denoted by a unique name. By default, the integer identifiers beginning from 1 to the number of rows are assigned to the data frame by default. The task here is to change the Rows names in given dataframe using R programming. Dataset in use: First SecondThird1a72a
3 min read