[R] column-wise deletion in data-frames

jhainm@fas.harvard.edu jhainm at fas.harvard.edu
Mon Jul 18 14:38:31 CEST 2005


I have a huge dataframe and like to delete all those variables from it that that
have NAs. The deletion of vars should be done column-wise, and not row-wise as
na.omit would do it, because I have some vars that have NAs for all rows thus
using na.omit I would end up with no obs. Is there a convenient way to do this

To make the question more explicit. Imagine a dataset that looks something like
this (but much bigger)

X1 <- rnorm(1000)
X2 <- c(rep(NA,1000))
X3 <- rnorm(1000)
X4 <- c(rep(NA,499),1,44,rep(NA,499))
X5 <- rnorm(1000)

data <- as.data.frame(cbind(X1,X2,X3,X4,X5))

So only X1, X3 and X5 are vars without any NAs and there are some vars (X2 and
X4 stacked in between that have NAs). Now, how can I extract those former vars
in a new dataset or remove all those latter vars in between that have NAs
(without missing a single row)?

Thank you very much!


More information about the R-help mailing list