Reputation: 28905
I'm trying to remove all the rows from my data frame in which the 3rd column is NA:
new.frame <- data[(!is.na(data$z)),]
But I'm getting an error.
Warning message:
In is.na(data$z) : is.na() applied to non-(list or vector) of type 'NULL'
How can I accomplish this?
Upvotes: 1
Views: 6688
Reputation: 503
Try this:
new.frame.nonull <- data[(!is.null(data$z)),]
new.frame <- new.frame.nonull[(!is.na(new.frame.nonull$z)),]
Upvotes: 0
Reputation: 69241
There's also complete.cases()
which may be easier to read. Using Dirk's data:
new.frame2 <- DF[complete.cases(DF) ,]
> all.equal(new.frame, new.frame2)
[1] TRUE
Upvotes: 3
Reputation: 368599
Reproducible examples, please. Here is one that works:
R> set.seed(42)
R> DF <- data.frame(a=rnorm(10), b=sample(LETTERS, 10, replace=TRUE),
+> z=cumsum(runif(10)))
R> DF[c(2,4,6),"z"] = NA
R> DF
a b z
1 1.3709584 X 0.737596
2 -0.5646982 D NA
3 0.3631284 Z 1.936759
4 0.6328626 Y NA
5 0.4042683 C 2.625877
6 -0.1061245 N NA
7 1.5115220 K 3.466127
8 -0.0946590 X 3.673786
9 2.0184237 L 4.580388
10 -0.0627141 V 5.192166
R> new.frame <- DF[(!is.na(DF$z)),]
R> new.frame
a b z
1 1.3709584 X 0.737596
3 0.3631284 Z 1.936759
5 0.4042683 C 2.625877
7 1.5115220 K 3.466127
8 -0.0946590 X 3.673786
9 2.0184237 L 4.580388
10 -0.0627141 V 5.192166
R>
Upvotes: 9