Finding the number of NA values ​​for a combination of columns in R

Suppose I have a dataset as follows:

(dd <- read.table(header = TRUE, text="a    b
1    2
NA   1
1    NA
NA   NA
1    2
NA   3"))

#    a  b
# 1  1  2
# 2 NA  1
# 3  1 NA
# 4 NA NA
# 5  1  2
# 6 NA  3

I think how we can get the number of NA values ​​for a combination of two columns. My output should look like this:

No NA - 2
1st column NA - 2
2nd column NA - 1
Both NA - 1

I do not understand how to do this for a combination of columns. Can anybody help me?

+4
source share
1 answer

table up:

table(lapply(dd, is.na))

#       b
#a       FALSE TRUE
#  FALSE     2    1
#  TRUE      2    1

And if you need a vector for the purposes of a subset, as it interactiongives:

interaction(lapply(dd,is.na))
#[1] FALSE.FALSE TRUE.FALSE  FALSE.TRUE  TRUE.TRUE   FALSE.FALSE TRUE.FALSE 
#Levels: FALSE.FALSE TRUE.FALSE FALSE.TRUE TRUE.TRUE

You can do:

vec <- c("none","first","second","both")[interaction(lapply(dd,is.na))]
#[1] none   first  second both   none   first

table(vec)
#vec
#  none  first second   both 
#     2      2      1      1 
+7
source

Source: https://habr.com/ru/post/1616651/


All Articles