Hi, say I have a vector: v <- c(1,2,3,NA,5,6,NA,7) And I would like to set the elements that are NA to, say, 0 I can use is.na() to get a logical vector: ind <- is.na(v) Is there a way in which I can use this logical vector to set the NA elements in v to 0? Thanks, ------------------------------------------------------------------- Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE ------------------------------------------------------------------- All great ideas are controversial, or have been at one time.
v[is.na(v)]=0 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Rajarshi Guha Sent: Friday, November 21, 2003 2:38 PM To: R Subject: [R] using a logical vector as a mask? Hi, say I have a vector: v <- c(1,2,3,NA,5,6,NA,7) And I would like to set the elements that are NA to, say, 0 I can use is.na() to get a logical vector: ind <- is.na(v) Is there a way in which I can use this logical vector to set the NA elements in v to 0? Thanks, ------------------------------------------------------------------- Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE ------------------------------------------------------------------- All great ideas are controversial, or have been at one time. ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On 21 Nov 2003 at 0:38, Rajarshi Guha wrote:> v <- c(1,2,3,NA,5,6,NA,7) > ind <- is.na(v) > ind[1] FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE> v[ind] <- 0 > v[1] 1 2 3 0 5 6 0 7 Kjetil Halvorsen> Hi, > say I have a vector: > > v <- c(1,2,3,NA,5,6,NA,7) > > And I would like to set the elements that are NA to, say, 0 > > I can use is.na() to get a logical vector: > > ind <- is.na(v) > > Is there a way in which I can use this logical vector to set the NA > elements in v to 0? > > Thanks, > > ------------------------------------------------------------------- > Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> GPG Fingerprint: > 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE > ------------------------------------------------------------------- > All great ideas are controversial, or have been at one time. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help