Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding count of NA values for combination of columns in R

Tags:

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 am thinking how we can get the count of NA values for combination of two columns. My output should be like,

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

I am not getting an idea how to do this for a combination of columns. Can anybody help me?

like image 511
haimen Avatar asked Mar 14 '23 02:03

haimen


1 Answers

table it up:

table(lapply(dd, is.na))

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

And if you need a vector for subsetting purposes, since interaction gives:

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 
like image 157
thelatemail Avatar answered Mar 16 '23 15:03

thelatemail