Sunday, October 3, 2010

R removing NA

http://www.opensubscriber.com/message/r-help@stat.math.ethz.ch/7268077.html

> df<-data.frame(name=c('a','b'), age=c('1','2'))
> if (any(apply(df,1,function(x) any(is.na(x)))) == TRUE)  { r <- df[-which(apply(df,1,function(x) any(is.na(x)))),] }  else { r <- df }
> r
  name age
1    a   1
2    b   2

> df<-data.frame(name=c('a','b'), age=c('1',NA))
> df
  name  age
1    a    1
2    b
> if (any(apply(df,1,function(x) any(is.na(x)))) == TRUE)  { r <- df[-which(apply(df,1,function(x) any(is.na(x)))),] }  else { r <- df }
> r
  name age
1    a   1


## Remove rows where Year=NA from the dataframe
df <- df [!is.na(df$Year),] 

No comments: