R中具有多个分组因子的多个变量的均值和sd

我一直在寻找答案,但仍然没有找到解决方案,我对R还是陌生的。 我的数据框显示了不同条件下〜70种植物的一种生态特征(相对土壤覆盖率)的测量值:不同的年份,不同的化学处理方法以及是否存在温室。

I need to summarize that data into a new dataframe that displays the mean and sd of the trait for each species and for each combination of factors (conditions). I know aggregate or lapply could help but i struggle to combine both grouping by 3 different factors and the multiples species, which implies the need of an "automated" code.

如果我错过了回答我的问题的帖子,对不起

感谢您的耐心配合和帮助

评论
  • 輸給o時咣
    輸給o時咣 回复

    With data.table, you can do something in this flavor:

    library(data.table)
    setDT(df)
    df[, lapply(.SD, function(x) return(c(mean(x), sd(x))),
                     by = c("col1","col2"),
                    .SDcols = c("x1","x2")]
    

    (没有可复制的示例,很难为您提供更精确的语法)

    This means: apply mean and sd for each subset of data (here columns x1 and x2) by groups (col1 and col2)