我的数据格式如下:
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"
我想将df1
转换为以下格式:
id descx descy descz
[1,] "100" "aaa" "bbb" "ccc"
[2,] "200" "ddd" "eee" "fff"
[3,] "300" "ggg" "hhh" "iii"
我尝试reshape2
和tidyr
个数据库来转移/转换df1
是徒劳的。我还在此论坛中检查并测试了这些和其他帖子(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;)的建议表格,但无法获得我需要的输出。有什么建议可以满足我的需求吗?