r - long to wide with out timevar -
this question has answer here:
i move data.frame long wide. whenever have duplicate id, want copy results in particular var new col. in end want id unique id. have looked @ using reshape function - can't seem how handle not having "timevar" - in particular case, don't have grouping var want reshape based on. there ton on stack overflow reshaping data can't seem find issue.
i have:
a<- data.frame( id = c( 11,12,13,14,15,15,16,17,17,18,19,10) , pi = c( 21:32 ) )
i want
b<- data.frame( id = c( 11,12,13,14,15,16,17,18,19,10) , pi = c( 21:25,27:28,30:32 ), pi2 = c( na,na,na,na,26,na,29,na,na,na) )
you can in few steps:
bb <- aggregate(pi~id, data=a, fun=c) cols <- max(sapply(bb$pi, length)) b <- cbind(bb$id, sapply(seq(cols), function(i) sapply(bb$pi, '[', i))) b ## [,1] [,2] [,3] ## 1 10 32 na ## 2 11 21 na ## 3 12 22 na ## 4 13 23 na ## 5 14 24 na ## 6 15 25 26 ## 8 16 27 na ## 9 17 28 29 ## 11 18 30 na ## 12 19 31 na
Comments
Post a Comment