i have data frame df1
5 columns c1
, c2
, c3
, c4
, , c5
:
df1 <- data.frame(c1 = c(1, 0, 1), c2 = c(0, 0, 1), c3 = c(2, 1, 0), c4 = c(1, 4, 3), c5 = c(1, 0, 0)) #df1 c1 c2 c3 c4 c5 1 0 2 1 1 0 0 1 4 0 1 1 0 3 0
my desired output is:
1 3 3 4 5 3 4 4 4 4 1 2 4 4 4
basically, creating matrix based on column number , value (value less or equal 5). example, 2nd row in df1
has 1
, 4
belongs column c3
, c4
. so, 2nd row in desired matrix 3 4 4 4 4
.
i have tried myself. code involves several nested control flows , not working large data frame.
a little apply
magic:
t(apply(df, 1, function(x) rep(row(as.matrix(x)), x))) # [,1] [,2] [,3] [,4] [,5] #[1,] 1 3 3 4 5 #[2,] 3 4 4 4 4 #[3,] 1 2 4 4 4
Comments
Post a Comment