невозможно использовать aggregate () с data.frame в режиме 'list' - PullRequest
1 голос
/ 19 мая 2019

Предварительные шаги:

#======================
# 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 , который является data.frame из Mode 'list'?

> 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

Спасибо

1 Ответ

1 голос
/ 19 мая 2019

Это list с одним столбцом data.frame, и имена также изменяются. Один из вариантов - преобразовать его обратно в один data.frame, cbind используя элементы list, а затем применить 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
...