对此进行了几个小时的研究,并查看了我能找到的所有dcast
答案和教程。我想我最好再问一下。
首先显示示例数据:
xx = data.frame(SOIL = c(rep("kraz", 20), (rep("sodo", 20))),
DM = runif(0, 20,n=40),
cutnum = c(rep(1:4,10)))
现在,所需的操作。我希望最终得到一个表格中包含Soil
个名称的表格,以及Cutnum
作为列名称,DM
列在cutnumbers
下的列中}。
# Soil 1 2 3 4
# kraz 1.2 19 12.1 9.9
# kraz 15.3 4.5 9.2 12.1
# kraz 14 15.2 5.2 15.4
# kraz 18.5 0.7 14.3 5
# kraz 17.1 15.8 2.9 9.5
# kraz 13 14.4 4.9 8.6
# kraz 3 10.2 3.5 14
# kraz 17.7 8.6 10.6 16.1
# kraz 12.6 1.7 2.2 17.5
# kraz 3.8 16.7 4.8 0.4
# kraz 4.1 17.1 12.5 14.5
# kraz 17.8 5.2 11.2 9.5
# kraz 12.3 2.2 4.8 8.7
# kraz 7.3 3 10.2 1.6
# kraz 11.3 12.2 13.4 10.2
# kraz 7.5 15.9 8.9 18.3
# kraz 15 5 19.6 16.5
# sodo 8.4 2.6 18.3 15.1
# sodo 6.9 19.7 6.5 8.4
# sodo 4 6.5 4.2 11.9
# sodo 0.8 12 18.3 15.4
# sodo 7.2 11.9 6.7 4.7
# sodo 2.6 4.4 13.8 13.7
# sodo 11.3 16.4 12.3 9.6
# sodo 5.6 17.1 11.4 16.7
# sodo 10.4 4.7 5.7 10.6
# sodo 8.7 5.6 1.1 4.8
# sodo 19.2 14.8 7 7
# sodo 18.6 9 14.9 5
# sodo 4.3 2.4 0.3 11.1
# sodo 4.9 18.4 19.5 9.7
# sodo 18.8 3.3 15.9 12.7
# sodo 19.7 0.1 13.6 3
# sodo 11.3 11.1 6.6 9.5
# sodo 8.1 11.3 10.1 3.5
# sodo 14.1 13.5 0.5 17.2
# sodo 16.8 15.6 16.2 17.3
我试过了:
require(reshape2)
dcast(xx, formula = SOIL ~ Cutnum, value.var = xx$DM)
产生以下内容:
Error: value.var (18.943128376267911.662011714652217.372458190657214.7615862498069.991016136482364.527641483582569.107870771549641.0582387680187810.695438273251115.275471545755917.17561007011680.9180781804025171.6045100009068812.556012449786118.57340626884257.465867823921144.576288489624868.3055530954152315.88032039348041.3668353855609916.888091783039310.544018512591714.95763068087410.46029578894380.95387450419366410.41133180726329.8472668603062618.449066961184111.24195748940114.0428098617121613.89849389437598.8408243283629416.336669707670818.53340925183156.113082133233555.875797253102060.06016504485160119.295095484703784.955938421189798.97169086616486) not found in input
In addition: Warning message:
In if (!(value.var %in% names(data))) { :
the condition has length > 1 and only the first element will be used
我非常感谢能够获得更有用结果的建议。
答案 0 :(得分:2)
根据评论,似乎OP要求行没有函数聚合排序的宽格式。 这应该这样做,
set.seed(123)
xx = data.frame(SOIL = c(rep("kraz", 20), (rep("sodo", 20))),
DM = runif(0, 20,n=40),
cutnum = c(rep(1:4,10)))
require(reshape2)
xx$t <- rep(1:10, each=4) # Add column to identify subset
dcast(xx, SOIL+t~cutnum, value.var="DM")[, -2] # Remove new column