r - Duplication of elements from a data frame -


i have data frame looks this:

x <- as.data.frame(matrix(data = c("a", "b", "c", 1, 2, 3),                        ncol = 2,                       nrow = 3,                       byrow = false))  > x   v1 v2 1  1 2 b  2 3 c  3 

lets want duplicate element x[1,2] 4 times, element x[2,2] 6 times , element x[3,2] 5 times , save them in new data frame.

> v   v1   1   1   1   1   2   2   ... 

i know using rep, i'm wondering if there more comfortable way.

here's alternative

data.frame(v=rep(as.numeric(x[,2]), c(4,6,5))) 

if want keep factors, omit as.numeric(·)

data.frame(v=rep(x[,2], c(4,6,5))) 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -