You’ll usefulness refer to modes to duplicate rows in an information body in R the usage of purposes from the dplyr bundle:
Form 1: Reflect Each and every Row the Similar Choice of OccasionsÂ
library(dplyr) #mirror every row thrice df %>% slice(rep(1:n(), every = 3))
Form 2: Reflect Each and every Row a Other Choice of Occasions
library(dplyr) #mirror the primary row thrice and the second one row 5 instances df %>% slice(rep(1:n(), instances = c(3, 5)))
Refer to examples display tips on how to usefulness every mode in follow.
Instance 1: Reflect Each and every Row the Similar Choice of Occasions
Think we’ve got refer to information body with two rows in R:
#develop information body
df <- information.body(staff=c('A', 'B'),
issues=c(10, 15),
rebounds=c(4, 8),
assists=c(2, 5))
#view information body
df
staff issues rebounds assists
1 A 10 4 2
2 B 15 8 5
We will be able to usefulness refer to syntax to copy every row within the information body thrice:
library(dplyr)
#develop unused information body that repeats every row in actual information body thrice
new_df <- df %>% slice(rep(1:n(), every = 3))
#view unused information body
new_df
staff issues rebounds assists
1 A 10 4 2
2 A 10 4 2
3 A 10 4 2
4 B 15 8 5
5 B 15 8 5
6 B 15 8 5
Realize that every of the rows from the actual information body were repeated thrice.
Instance 2: Reflect Each and every Row a Other Choice of Occasions
Think we’ve got refer to information body with two rows in R:
#develop information body
df <- information.body(staff=c('A', 'B'),
issues=c(10, 15),
rebounds=c(4, 8),
assists=c(2, 5))
#view information body
df
staff issues rebounds assists
1 A 10 4 2
2 B 15 8 5
We will be able to usefulness refer to syntax to copy the primary row thrice and the second one row 5 instances:
library(dplyr)
#develop unused information body that repeats first row thrice and 2d row 5 instances
new_df <- df %>% slice(rep(1:n(), instances = c(3, 5)))
#view unused information body
new_df
staff issues rebounds assists
1 A 10 4 2
2 A 10 4 2
3 A 10 4 2
4 B 15 8 5
5 B 15 8 5
6 B 15 8 5
7 B 15 8 5
8 B 15 8 5
Realize that the primary row within the actual information body has been repeated thrice and the second one row has been repeated 5 instances.
Alike: Find out how to Utility the slice() Serve as in dplyr
Spare Assets
Refer to tutorials give an explanation for tips on how to carry out alternative familiar operations in dplyr:
Find out how to Make a choice Columns by means of Index The use of dplyr
Find out how to Make a choice the First Row by means of Crew The use of dplyr
Find out how to Clear out by means of More than one Situations The use of dplyr
Find out how to Clear out Rows that Comprise a Sure Anecdote The use of dplyr