根据字符和缺失的条件计算每行的列数

我想按字符和缺失条件计算每一行的列数。

For example, I have this dataset, test.

I want to create num columns, counting the number of columns 'not' in missing or empty value.

a<-c("aa","bb","cc","dd","",NA)
b<-c("",NA,"aa","","","dd")
c<-c("aa","",NA,NA,"cc","dd")
d<-c("aa","bb","",NA,"cc","dd")
test<-data.frame(cbind(a,b,c,d))

     a    b    c    d
1   aa        aa   aa
2   bb <NA>        bb
3   cc   aa <NA>     
4   dd      <NA> <NA>
5             cc   cc
6 <NA>   dd   dd   dd

I want to count the number of columns containing NA and empty value like

     a    b    c    d   num
1   aa        aa   aa   3
2   bb <NA>        bb   2
3   cc   aa <NA>        2
4   dd      <NA> <NA>   1
5             cc   cc   2
6 <NA>   dd   dd   dd   3

I tried some approach in other posts, like rowSums

Count number of columns by a condition (>) for each row

> test$num<-rowSums(test!=c("",NA),na.rm=T)
> test
     a    b    c    d num
1   aa        aa   aa   3
2   bb <NA>        bb   0
3   cc   aa <NA>        2
4   dd      <NA> <NA>   0
5             cc   cc   2
6 <NA>   dd   dd   dd   0

但是,它返回了错误的数字,我找不到原因。

您能让我知道如何解决这个问题吗?

评论
  • 也,許
    也,許 回复

    Another idea using rowSums is to replace empty with NA, i.e.

    rowSums(!is.na(replace(test, test == '', NA)))
    #[1] 3 2 2 1 2 3
    
  • funde
    funde 回复

    You could use rowSums to count number of NAs or empty values in each row and then subtract it from number of columns in the dataframe.

    test$num <- ncol(test) - rowSums(is.na(test) | test == "")
    test
    #     a    b    c    d num
    #1   aa        aa   aa   3
    #2   bb <NA>        bb   2
    #3   cc   aa <NA>        2
    #4   dd      <NA> <NA>   1
    #5             cc   cc   2
    #6 <NA>   dd   dd   dd   3