R中来自dplyr的汇总数据帧的算术
Posted
技术标签:
【中文标题】R中来自dplyr的汇总数据帧的算术【英文标题】:Arithmetic on summarized dataframe from dplyr in R 【发布时间】:2017-06-09 13:36:10 【问题描述】:我有一个大型数据集,我使用 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))
【问题讨论】:
data.sum %>% summarise_if(is.numeric, mean)
Why does subsetting a data frame vs. a tibble give different results的可能重复
【参考方案1】:
我们可以使用[[
将列提取为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)
【讨论】:
感谢多列的 dplyr 提示。以上是关于R中来自dplyr的汇总数据帧的算术的主要内容,如果未能解决你的问题,请参考以下文章
R语言算术运算符(Arithmetic Operators+-*/%%%/%^)R语言算术运算符(Arithmetic Operators)实战示例