[R] replace

Jeff Gentry jgentry at jimmy.harvard.edu
Fri Oct 11 21:00:08 CEST 2002


Following up on my own message, sorry I was just removing NAs, sorry.

> A vector?
> > a <- c(1,2,NA,3,4,NA,5,6,NA)
> > a
> [1]  1  2 NA  3  4 NA  5  6 NA

> a[is.na(a)] <- 0
> a
[1] 1 2 0 3 4 0 5 6 0

> A list?
> > a <- list(1,2,3,NA,4,5,NA,6,NA,7)
> a <- lapply(a,function(x){if(is.na(x))return(0) else(return(x))})
> a 
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 0

[[5]]
[1] 4

[[6]]
[1] 5

[[7]]
[1] 0

[[8]]
[1] 6

[[9]]
[1] 0

[[10]]
[1] 7


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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