Thursday, 9 July 2015

R: Removing missing values


Removing missing values from vector
> vector1 <- c(123, NA, 345, 3, 4, 5, NA, NA, 10)
> vector1
[1] 123  NA 345   3   4   5  NA  NA  10
> 
> missing <- is.na(vector1)
> missing
[1] FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE
> 
> vector1[!missing]
[1] 123 345   3   4   5  10

“missing” is a logical vector, which is TRUE if element is NA, else false.
vector1[!missing] returns all members without missing values.

Remove rows with missing data
By using “na.omit()” function we can remove rows with missing data. “na.omit” returns the object with incomplete cases removed. Lets see this with inbuilt dataset airquality.

> x <- na.omit(airquality)


Above statement remove rows with missing data.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment