我有一个大型数据集,我使用dplyr()
summarize
来生成一些方法。
偶尔,我想对该输出执行算术运算。
例如,我想从下面的输出中得到平均值,比如" m.biomass"。
我已尝试过此mean(data.sum[,7])
和此mean(as.list(data.sum[,7]))
。有没有快速简便的方法来实现这一目标?
data.sum <-structure(list(scenario = c("future", "future", "future", "future"
), state = c("fl", "ga", "ok", "va"), m.soc = c(4090.31654013689,
3654.45350562628, 2564.33199749487, 4193.83388887064), m.npp = c(1032.244475,
821.319385, 753.401315, 636.885535), sd.soc = c(56.0344229400332,
97.8553643582118, 68.2248389927858, 79.0739969429246), sd.npp = c(34.9421782033153,
27.6443555578531, 26.0728757486901, 24.0375040705595), m.biomass = c(5322.76631158111,
3936.79457763176, 3591.0902359206, 2888.25308402464), sd.m.biomass = c(3026.59250918009,
2799.40317348016, 2515.10516340438, 2273.45510178843), max.biomass = c(9592.9303,
8105.109, 7272.4896, 6439.2259), time = c("1980-1999", "1980-1999",
"1980-1999", "1980-1999")), .Names = c("scenario", "state", "m.soc",
"m.npp", "sd.soc", "sd.npp", "m.biomass", "sd.m.biomass", "max.biomass",
"time"), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4), vars = list(quote(scenario)), labels = structure(list(
scenario = "future"), class = "data.frame", row.names = c(NA,
-1), vars = list(quote(scenario)), drop = TRUE, .Names = "scenario"), indices = list(0:3))
答案 0 :(得分:2)
我们可以使用[[
将列提取为vector
;由于mean
仅适用于vector
或matrix
- 而非data.frame
。如果OP希望在单个列上执行此操作,请使用:
mean(data.sum[[7]])
#[1] 3934.726
如果只有data.frame
课程,则data.sum[,7]
会将其解除为vector
,但tbl_df
会阻止其将其展开为vector
}
对于多列,dplyr
也有专门的功能
data.sum %>%
summarise_each(funs(mean), 3:7)