【问题标题】:unable to use aggregate() with data.frame of mode 'list'无法将聚合()与模式“列表”的 data.frame 一起使用
【发布时间】:2019-10-05 20:23:31
【问题描述】:

初步步骤:

#======================
# added ‘height’ column to the in-built data.frame: CO2
height <- runif(84, 30.0, 44)
cbind(CO2, height)
#======================

聚合 CO2 数据框 产生正确的结果:

    > aggregate(cbind(height,uptake)~conc, CO2, mean)
      conc   height   uptake
    1   95 37.04813 12.25833
    2  175 38.14815 22.28333
    3  250 34.70362 28.87500
    4  350 32.81782 30.66667
    5  500 37.19268 30.87500
    6  675 36.16915 31.95000
    7 1000 37.33184 33.58333

Alternatively,
> aggregate(CO2[,cbind("height","uptake")], by = list(CO2$conc), FUN = mean)
  Group.1   height   uptake
1      95 37.04813 12.25833
2     175 38.14815 22.28333
3     250 34.70362 28.87500
4     350 32.81782 30.66667
5     500 37.19268 30.87500
6     675 36.16915 31.95000
7    1000 37.33184 33.58333

但是,当我将 CO2 转换为列表时:

> CO2list <- lapply(CO2, as.data.frame)
> summary(CO2list)
          Length Class      Mode
Plant     1      data.frame list
Type      1      data.frame list
Treatment 1      data.frame list
conc      1      data.frame list
uptake    1      data.frame list
height    1      data.frame list

但是,对于 CO2list,我收到以下四次 aggregate() 尝试的错误。

问题:如何使用 CO2list 进行聚合,它是 Modedata.frame '列表'?

> aggregate(cbind(height,uptake)~conc, CO2list, mean)
Error in model.frame.default(formula = cbind(height, uptake) ~ conc, data = CO2list) : 
  invalid type (list) for variable 'cbind(height, uptake)'

> aggregate(CO2list[,cbind("height","uptake")], by = list(CO2list$conc), FUN = mean)
Error in CO2list[, cbind("height", "uptake")] : 
  incorrect number of dimensions
> aggregate(cbind(height,uptake), by = list(CO2list$conc), FUN = mean)
Error in cbind(height, uptake) : object 'uptake' not found
> aggregate(cbind(CO2list$height,CO2list$uptake), by = list(CO2list$conc), FUN = mean)
Error in aggregate.data.frame(cbind(CO2list$height, CO2list$uptake), by = list(CO2list$conc),  : 
  arguments must have same length

谢谢

【问题讨论】:

    标签: r list dataframe aggregate mode


    【解决方案1】:

    这是一个list,带有单列data.frame,名称也发生了变化。一种选择是通过cbinding list 元素将其转换回单个data.frame,然后应用aggregate

    newDat <- setNames(do.call(cbind, CO2list), names(CO2list))
    aggregate(cbind(height,uptake)~conc, newDat, mean)
    #  conc   height   uptake
    #1   95 39.15248 12.25833
    #2  175 35.38677 22.28333
    #3  250 38.56924 28.87500
    #4  350 37.73494 30.66667
    #5  500 35.37963 30.87500
    #6  675 36.26344 31.95000
    #7 1000 36.43538 33.58333
    

    或者提取 list 元素并在 aggregate 中使用它

    aggregate(cbind(height, uptake = CO2list[["uptake"]][[1]]), 
          list(conc = CO2list[["conc"]][[1]]), FUN = mean)
    #  conc   height   uptake
    #1   95 39.15248 12.25833
    #2  175 35.38677 22.28333
    #3  250 38.56924 28.87500
    #4  350 37.73494 30.66667
    #5  500 35.37963 30.87500
    #6  675 36.26344 31.95000
    #7 1000 36.43538 33.58333
    

    【讨论】:

    • 感谢您的解决方案。请问您是如何在第二个“聚合”中仅使用“高度”的?我原以为第二个聚合应该是:aggregate(cbind(height = CO2list[["height"]][[1]], uptake = CO2list[["uptake"]][[1]]), list(conc = CO2list[["conc"]][[1]]), FUN = mean) 另一方面,当我尝试 cbind() 时,我得到:aggregate(cbind(height, uptake), + list(conc = CO2list[["conc"]][[1]]), FUN = mean) Error in cbind(height, uptake) : object 'uptake' not found 谢谢!
    • @analytics-josh 根据您的代码,height 不是作为数据集中的列创建的,而是全局环境中的一个对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 2016-02-10
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多