I have data in the following format:
df1 <- cbind(allnames = c("id","descx","descy","descz","id","descx","descy","descz","id","descx","descy","descz"),
allvalues = c(100,"aaa","bbb", "ccc", 200,"ddd","eee", "fff",300,"ggg","hhh", "iii"))
> df1
allnames allvalues
[1,] "id" "100"
[2,] "descx" "aaa"
[3,] "descy" "bbb"
[4,] "descz" "ccc"
[5,] "id" "200"
[6,] "descx" "ddd"
[7,] "descy" "eee"
[8,] "descz" "fff"
[9,] "id" "300"
[10,] "descx" "ggg"
[11,] "descy" "hhh"
[12,] "descz" "iii"
I would like to Pivot/Transform df1
to the following format:
id descx descy descz
[1,] "100" "aaa" "bbb" "ccc"
[2,] "200" "ddd" "eee" "fff"
[3,] "300" "ggg" "hhh" "iii"
I tried reshape2
and tidyr
libraries to Pivot/Transform df1
in vain. I also checked and tested suggestion form these and other postings (R: Pivoting using 'spread' function; Pivot table on R using `dplyr` or `tidyr`; Using R, how to pivot/transform a dataset whose elements are the result of a function;) in this forum but could not get the output I needed. Any advice to accomplish my need please?