I have a data frame that looks like this:
x <- as.data.frame(matrix(data = c("a", "b", "c", 1, 2, 3),
ncol = 2,
nrow = 3,
byrow = FALSE))
> x
V1 V2
1 a 1
2 b 2
3 c 3
Lets say I want to duplicate element x[1,2] four times, element x[2,2] six times and element x[3,2] five times and save them all in a new data frame.
> v
V1
1
1
1
1
2
2
...
I know I could do it by using rep
, but i'm wondering if there is a more comfortable way.
Here's another alternative
data.frame(v=rep(as.numeric(x[,2]), c(4,6,5)))
If you want to keep the factors, then omit as.numeric(·)
data.frame(v=rep(x[,2], c(4,6,5)))