32

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,386
  • 16
  • 69
  • 103

2 Answers2

71

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
  • 170,733
  • 42
  • 434
  • 486
42

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
  • 168,168
  • 29
  • 327
  • 408