[R] replacing values in a vector

Kaspar Pflugshaupt pflugshaupt at geobot.umnw.ethz.ch
Thu Jul 26 14:23:12 CEST 2001

On Thursday 26 July 2001 13:42, Sven Garbade wrote:                           
> Hi all,
> there is a vector v with several NAs. I want to create a new vector n of
> the same length as v and the same NAs as in v and tried this:
> n <- vector(length=length(v), mode="numeric")
> replace(n, which(is.na(v)), NA)
> but this does't work, all values in n are 0. What went wrong?

You have to assign the result of the replacement, like in

n <- replace(n, which(is.na(v)), NA)

Or, somewhat simpler:

n[is.na(v)] <- NA


Kaspar Pflugshaupt


Kaspar Pflugshaupt
Geobotanical Institute
ETH Zurich, Switzerland
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