仅对非 NA 元素求和,但如果所有 NA 则返回 NA [英] sum non NA elements only, but if all NA then return NA
问题描述
我想我已经在评论中得到了很好的答案,但我会改写问题以供将来参考.
I think I already got really good answers on the comments, but I will rephrase the question for future reference.
我正在尝试使用 data.table 按组求和.问题是某些组只有有 NA.对于这些组,我希望总和返回 NA.但是,如果有一组的值与 NA 不同,我想得到非 NA 值的总和.
I am trying to sum by groups using data.table. The problem is that some groups only have NA. For these groups I would like the sum to return NA. However, if there is one group that has one value different that NA, I would like to get the sum of the non-NA values.
A <- data.table(col1= c('A','A','B','B','C','C'),
col2= c(NA,NA,2,3,NA,4))
这没有添加参数 na.rm = T
,C 组在应该返回 4 时返回 NA.
This without adding the argument na.rm = T
, group C returns NA when it should return 4.
A[, sum(col2), by = .(col1)]
col1 V1
1: A NA
2: B 5
3: C NA
但是,添加 na.rm = T
在应该返回 NA 的情况下在 A 组中返回 0.
However, adding na.rm = T
returns 0 in group A when it should return NA.
A[, sum(col2, na.rm = T), by = .(col1)]
col1 V1
1: A 0
2: B 5
3: C 4
我最喜欢的方法是sandipan在评论中建议的方法,类似于我在下面写的函数:
The approach that i like the best is the one that sandipan suggested in the comments, which is akin to the function I wrote below:
ifelse(all(is.na(col2)), NA, sum(col2, na.rm = T)
我创建了一个函数来解决它,但我不确定是否有一个已经内置的方法来解决这个问题:
I created a function to get around it, but I am not sure whether there is an already built-in way to get around this:
sum.na <- function(df){
if (all(is.na(df))){
suma <- NA
}
else {
suma <- sum(df, na.rm = T)
}
return(suma)
}
推荐答案
根据其他用户的建议,我将发布我的问题的答案.该解决方案由@sandipan 在上面的评论中提供:
Following the suggestions from other users, I will post the answer to my question. The solution was provided by @sandipan in the comments above:
如问题中所述,如果您需要对包含 NA 的一列的值求和,有两种好方法:
As noted in the question, if you need to sum the values of one column which contains NAs,there are two good approaches:
1) 使用 ifelse:
1) using ifelse:
A[, (ifelse(all(is.na(col2)), col2[NA_integer_], sum(col2, na.rm = T))),
by = .(col1)]
2) 按照@Frank 的建议定义一个函数:
2) define a function as suggested by @Frank:
suma = function(x) if (all(is.na(x))) x[NA_integer_] else sum(x, na.rm = TRUE)
A[, suma(col2), by = .(col1)]
请注意,正如@Frank 指出的那样,我添加了 NA_integer_,因为我不断收到有关类型的错误.
Note that I added NA_integer_ as @Frank pointed out because I kept getting errors about the types.
这篇关于仅对非 NA 元素求和,但如果所有 NA 则返回 NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!