按组计算非NA值的数量 [英] Count number of non-NA values by group

查看:65
本文介绍了按组计算非NA值的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有这个数据框(df):

For example, I have this data frame (df):

Color    X1      X2    X3    X4
Red      1       1     0     2
Blue     0       NA    4     1 
Red      3       4     3     1
Green    2       2     1     0

我想创建一个计算"X2"中非 NA 数量的函数.按组(即按颜色").我希望在名为newdf的新数据框中输出此函数.这就是我想要的输出:

I would like to create a function that counts up the number of non-NAs in "X2" by group (i.e. by "color"). I would like the output of this function in a new data frame named newdf. This is what I would like for output:

Color    X2     
Red      2      
Blue     NA    
Green    1

到目前为止,我有以下代码:

So far, I have this code:

Question <- function(Color){
  Result <-
    rowsum((df[c("X2")] > 0) + 0, df[["X2"]], na.rm = TRUE) 
  rowSums(Result)[[Color]]
  }
  Question("Red") 

此函数提供的输出仅为 Question("Red")= 2 ,我想在一个新的数据帧(newdf)中获取所有颜色的结果.有人能帮忙吗?谢谢!

The output this function gives is just Question("Red")= 2 and I would like to instead get my results of all the colors in a new data frame (newdf). Can anyone help with this? Thanks!

推荐答案

或者如果您想使用data.table:

Or if you wanted to use data.table:

library(data.table)

dt[,sum(!is.na(X2)),by=.(Color)]

  Color V1
1:   Red  2
2:  Blue  0
3: Green  1

它也很容易在data.table中使用 ifelse()来获得蓝色(而不是0)的NA.请参阅:

Also its easy enough to use an ifelse() in your data.table to get an NA for blue instead of 0. See:

dt[,ifelse(sum(!is.na(X2)==0),as.integer(NA),sum(!is.na(X2))),by=.(Color)]

   Color V1
1:   Red  2
2:  Blue NA
3: Green  1

数据:

 dt <- as.data.table(fread("Color    X1      X2    X3    X4
Red      1       1     0     2
Blue     0       NA    4     1 
Red      3       4     3     1
Green    2       2     1     0"))

这篇关于按组计算非NA值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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