我想通过两个类为我的数据生成描述性统计数据:1)使用我的数据子集的“SampledSub”和“SampledLUL”:
myData <- structure(list(SampledLUL = structure(c(12L, 12L, 9L, 9L, 9L,
9L), .Label = c("citrus", "crop", "cypress swamp", "freshwater marsh and wet prairie",
"hardwood swamp", "improved pasture", "mesic upland forest", "mixed wetland forest",
"pineland", "rangeland", "shrub swamp", "urban", "xeric upland forest"), class = "factor"),
SampledSub = structure(c(12L, 12L, 4L, 12L, 8L, 4L), .Label = c("Aqualf", "Aquent",
"Aquept", "Aquod", "Aquoll", "Aquult", "Arent", "Orthod", "Psamment", "Saprist", "Udalf",
"Udult"), class = "factor"), SOC = c(3.381524292, 6.345916406, 2.122765119, 2.188488973,
6.980834272, 7.363643479)),
.Names = c("SampledLUL", "SampledSub", "SOC"), row.names = c(NA, 6L), class = "data.frame")
我用这个代码总结了两组:
group.test <- ddply(buffer, c("SampledSub", "SampledLUL"), summarise,
N = length(SOC),
mean = mean(SOC),
sd = sd(SOC),
se = sd / sqrt(N) )
但是输出表将组和摘要统计信息作为列.如何制作类似下图所示的表格?就我而言,“Sampledsub”将是观察结果,汇总统计数据将根据“SampledLUL”进行分组.
最佳答案 你可以用tidyr做它(虽然它不会像上面那样很好的输出表):
library(tidyr)
group.test %>% gather(variable, val, - SampledSub, -SampledLUL) %>%
unite(newcol, SampledLUL, variable) %>%
spread(newcol, val)
SampledSub pineland_mean pineland_N pineland_sd pineland_se urban_mean urban_N urban_sd urban_se
1 Aquod 4.743204 2 3.705861 2.620439 NA NA NA NA
2 Orthod 6.980834 1 NaN NaN NA NA NA NA
3 Udult 2.188489 1 NaN NaN 4.86372 2 2.096142 1.482196