1

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?

Head and toes
  • 639
  • 2
  • 11
  • 23

2 Answers2

1

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]),]
AF7
  • 2,561
  • 20
  • 51
0

If you want some other answer check complete.cases

  data[complete.cases(data),]
erasmortg
  • 3,152
  • 1
  • 15
  • 31