Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between complete.cases and !is.na

Tags:

r

na

I just discovered this new function it seems to me to an improved version of !is.na, maybe wrapped into an apply(df, 1). Am I correct or the following:

> a<-c(1,2,4,NA,6,8)
> identical(complete.cases(a), !is.na(a))
[1] TRUE

it's not always true?

like image 955
nigmastar Avatar asked Mar 24 '23 01:03

nigmastar


1 Answers

For an atomic vector, complete.cases and is.na will be identical. For more complex objects this will not be the case.

Eg, for, a data.frame is.na.data.frame will return a logical matrix of the same dimension as the input.

test <- data.frame(a, b =1)

is.na(test)
#          a     b
# [1,] FALSE FALSE
# [2,] FALSE FALSE
# [3,] FALSE FALSE
# [4,]  TRUE FALSE
# [5,] FALSE FALSE
#[6,] FALSE FALSE
complete.cases(test)
# [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
like image 50
mnel Avatar answered Apr 05 '23 11:04

mnel