36

Possible Duplicate:
R script - removing NA values from a vector

I could I remove all the NAs from a Vector using R?

[1]  1 NA  3 NA  5

Thank you

Community
  • 1
  • 1
Dail
  • 4,622
  • 16
  • 74
  • 109

2 Answers2

75

Use is.na with vector indexing

x <- c(NA, 3, NA, 5)
x[!is.na(x)]
[1] 3 5

I also refer the honourable gentleman / lady to the excellent R introductory manuals, in particular Section 2.7 Index vectors; selecting and modifying subsets of a data set

Andrie
  • 176,377
  • 47
  • 447
  • 496
45

In addition to @Andrie's answer, you can use na.omit

x <- c(NA, 3, NA, 5)
na.omit(x)
[1] 3 5
attr(,"na.action")
[1] 1 3
attr(,"class")
[1] "omit"
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418