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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With