仅对非NA元素求和,但如果所有NA都返回NA [英] sum non NA elements only, but if all NA then return NA

查看:439
本文介绍了仅对非NA元素求和,但如果所有NA都返回NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我对这些评论已经获得了很好的答案,但是我将重新表述该问题以供将来参考。

I think I already got really good answers on the comments, but I will rephrase the question for future reference.

我正在尝试使用数据对各组进行汇总。表。问题是某些组具有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 时,A组返回0应该返回NA。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆