R extracting non-missing data

11.5k views Asked by At

I have a dataframe:

a<-c(1,2,3)
b<-c("CarA","CarB",NA)
data<-data.frame(a,b)

data

    a    b
  1 1 CarA
  2 2 CarB
  3 3   NA

Now I want to remove the row with missing data (NA).

But this does not work:

data<-data[data[,2]!=NA,]

My thinking here is to look at the second column [,2] and look for those that don't have NA. Then extract the remaining data. Would someone be able to tell me what went wrong here?

2

There are 2 answers

0
AF7 On BEST ANSWER

Wouldn't

na.omit(data)

do? It seems the cleanest and fastest way to me.

By the way, your code does not work because you cannot do !=NA

Use is.na() instead (but na.omit() is better):

data[!is.na(data[,2]),]
0
erasmortg On

If you want some other answer check complete.cases

  data[complete.cases(data),]