How to use purrr package In R Language
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
Repeat Rows of DataFrame N Times in R
In this article, we will discuss how to repeat rows of Dataframe for a given number of times using R programming language.
Contact Us