[R] how to skip NA columns ?
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Thu Jun 20 14:28:50 CEST 2002
Prof Brian D Ripley <ripley at stats.ox.ac.uk> writes:
> Columns of what? A data frame or a matrix?
>
> For a data frame DF
>
> hasna <- sapply(DF, function(x) any(is.na(x)))
> DF <- DF[!hasna]
>
> and `hasna' tells which you removed. Use apply and matrix indexing for a
> matrix.
Actually, that works in either case, since is.na on a dataframe
is a matrix:
> data(airquality)
> apply(is.na(airquality),2,any)
Ozone Solar.R Wind Temp Month Day
TRUE TRUE FALSE FALSE FALSE FALSE
> hasna <- .Last.value
> airquality[1:10,!hasna]
Wind Temp Month Day
1 7.4 67 5 1
2 8.0 72 5 2
3 12.6 74 5 3
4 11.5 62 5 4
5 14.3 56 5 5
6 14.9 66 5 6
7 8.6 65 5 7
8 13.8 59 5 8
9 20.1 61 5 9
10 8.6 69 5 10
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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