Aggregate with na.action=na.pass gives unexpected answer

主宰稳场 提交于 2019-11-30 05:42:28

问题


I use the following data.frame as an example:

d <- data.frame(x=c(1,NA), y=c(2,3))

I'd like to sum up the values of y by the variable x. Since there is no common value of x, I would expect aggregation to just give me the original data.frame back, where NA is treated as a group. But aggregation gives me the following results.

>aggregate(y ~ x, data=d, FUN=sum)
  x y
1 1 2

I've read the documentation about changing the default actions of na.action, but it doesn't seem to give me anything meaningful.

>aggregate(y ~ x, data=d, FUN=sum, na.action=na.pass)
  x y
1 1 2

What is going on? I don't seem to understand what na.pass is doing in this case. Is there an option to accomplish what I want in R? Any help would be greatly appreciated.


回答1:


aggregate makes use of tapply, which in turn makes use of factor on its grouping variable.

But, look at what happens with NA values in factor:

factor(c(1, 2, NA))
# [1] 1    2    <NA>
# Levels: 1 2

Note the levels. You can make use of addNA to keep the NA:

addNA(factor(c(1, 2, NA)))
# [1] 1    2    <NA>
# Levels: 1 2 <NA>

Thus, you would probably need to do something like:

aggregate(y ~ addNA(x), d, sum)
#   addNA(x) y
# 1        1 2
# 2     <NA> 3

Or something like:

d$x <- addNA(factor(d$x))
str(d)
# 'data.frame': 2 obs. of  2 variables:
#  $ x: Factor w/ 2 levels "1",NA: 1 2
#  $ y: num  2 3
aggregate(y ~ x, d, sum)
#      x y
# 1    1 2
# 2 <NA> 3

(Alternatively, make the upgrade to something like "data.table", which will not just be faster than aggregate, but which will also give you more consistent behavior with NA values. No need to pay heed to whether you're using the formula method of aggregate or not.)

library(data.table)
as.data.table(d)[, sum(y), by = x]
#     x V1
# 1:  1  2
# 2: NA  3


来源:https://stackoverflow.com/questions/33783869/aggregate-with-na-action-na-pass-gives-unexpected-answer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!