r - Unstacking a data frame -


i have data frame looks this:

d <- c("a", "b", "c", "a", "b", "c", "a", "b", "c") par <- c("a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9") df1 <- as.data.frame(cbind(id, par)) 

i this:

a <- c("a1", "a4", "a7") b <- c("a2", "a5", "a8") c <- c("a3", "a6", "a7") df2 <- data.frame(rbind(a, b, c)) 

you unstack

as.data.frame(t(unstack(df1,par~id))) #  v1 v2 v3 #a a1 a4 a7 #b a2 a5 a8 #c a3 a6 a9 

or using dcast after creating sequence column 'id' group.

library(data.table)#v1.9.5 dcast(setdt(df1)[, ind:= 1:.n, id], id~ind, value.var='par') 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -