Ram
Ram

Reputation: 379

What is the difference between na.omit and is.na?

What is the difference between na.omit and is.na?

minO2 <- equal.count(na.omit(algae$mnO2),number=4,overlap=1/5)
stripplot(season ~ a3|minO2,data=algae[!is.na(algae$mnO2),])

I used na.omit instead of is.na in second code line? and I got totally different plot.

Another thing is that if you someone tell me the logic behind the ! (logical negation operator), I will be appreciated?

Upvotes: 1

Views: 2977

Answers (2)

Matthew Lundberg
Matthew Lundberg

Reputation: 42639

In the call to equal.count, the object na.omit(algae$mnO2) will be those values in algae$mn02 that are not NA.

Now, say that you have this code for the plot:

stripplot(season ~ a3|minO2,data=na.omit(algae))

If there are any columns of algae that contain NA in rows where algae$mn02 is not NA, the rows will not line up, and the plot will not be as expected.

Here's an example where this will happen:

algae<- data.frame(a3=c(NA,1,2), mn02=c(1,2,NA))
algae
##   a3 mn02
## 1 NA    1
## 2  1    2
## 3  2   NA

Note the difference between the following two expressions:

na.omit(algae)
##   a3 mn02
## 2  1    2

algae[!is.na(algae$mn02),]
##   a3 mn02
## 1 NA    1
## 2  1    2

The latter will line up with the shingle produced by equal.count(na.omit(algae$mn02)) but the former will not. The first expression here has one less row because there is an incomplete case where mn02 is not NA.

Note:

equal.count(na.omit(algae$mn02))
## 
## Data:
## [1] 1 2

...

There are two elements here. This expression does not check for NA in columns other than mn02.

Upvotes: 2

symbiotic
symbiotic

Reputation: 373

Set up the data

nas <- c(NA, 1, 2, 3, NA)

is.na returns a logial vector

is.na(nas)
[1]  TRUE FALSE FALSE FALSE  TRUE

The ! operator will take the trues and make them falses and turn the falses into trues.

!(is.na(nas))
[1] FALSE  TRUE  TRUE  TRUE FALSE

Which returns the negation of is.na(nas)

na.omit removes the the NA's from the vector

na.omit(tf)
[1] 1 2 3

I can't tell what's happening with your plots without a bit more information.

Upvotes: 2

Related Questions