[英]Summarize data by 2 groups
我想通過兩個類為我的數據生成描述性統計數據: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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.