[R] how to skip NA columns ?

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu Jun 20 17:20:12 CEST 2002



"Czerminski, Ryszard" wrote:
> 
> It works for me for data frame very nicely, but I am not quite sure how to
> get it to work for a matrix ?
> 
> R
> 
> > df
>   V1 V2 V3
> 1  1 NA  7
> 2  2 NA  8
> 3  3 NA  9
> > mask <- sapply(df, function(x) any(is.na(x)))
> > df[!mask]
>   V1 V3
> 1  1  7
> 2  2  8
> 3  3  9
> > m <- as.matrix(df)
> > m
>   V1 V2 V3
> 1  1 NA  7
> 2  2 NA  8
> 3  3 NA  9
> > mask <- apply(m, 2, function(x) any(is.na(x)))
> > m[!mask]
>    1    3 <NA> <NA> <NA> <NA>
>    1    3   NA   NA    7    9
> > dim(m[!mask])
> NULL

A comma will help:

  m[ , !mask]  

Uwe
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list