R count NA by group

后端 未结 2 1313
别那么骄傲
别那么骄傲 2020-12-03 21:59

Could someone please explain why I get different answers using the aggregate function to count missing values by group? Also, is there a better way to count missing values

相关标签:
2条回答
  • 2020-12-03 22:38
    library(dplyr)
    library(tidyr)
    
    #say you want to get missing values from group 1
    dataframe %>% filter(group = 1 & is.na(another_column))
    
    #missing values from group 2
    dataframe %>% filter(group = 2 & is.na(another_column))
    
    0 讨论(0)
  • 2020-12-03 22:52

    The help page at ?aggregate points out that the formula method has an argument na.action which is set by default to na.omit.

    na.action: a function which indicates what should happen when the data contain NA values. The default is to ignore missing values in the given variables.

    Change that argument to NULL or na.pass instead to get the results you are probably expecting:

    # aggregate(X ~ YEAR, data=DF, function(x) {sum(is.na(x))}, na.action = na.pass)
    aggregate(X ~ YEAR, data=DF, function(x) {sum(is.na(x))}, na.action = NULL)
    #   YEAR X
    # 1 2000 1
    # 2 2001 3
    # 3 2002 0
    
    0 讨论(0)
提交回复
热议问题