Re: [R] Logical function

2010-01-14 Thread Romain Francois
Here's one way : ds <- data.frame( st = runif(100), s0=runif(100), s1=runif(100),s2=runif(100),mp=runif(100)) ds <- within( ds, { n1 <- 1*( st>0.38 ) n2 <- numeric( length( st ) ) n2[ is.na(st) | st <= 0.38 ] <- .25 n2[ s0 == mp ] <- .25 n2[ s2 =

Re: [R] Logical function to turn missing values to 0's

2009-01-15 Thread ONKELINX, Thierry
Use the is.na function to test for NA values. And do read about vectorizing your code. You don't need loops for this problem. Without loops your code will run more than 400 times faster! > n <- 1000 > x <- matrix(data=rep(c(1,2,3,NA), n), ncol=n, nrow=n) > > system.time({ + y <- matrix(data=0

Re: [R] Logical function to turn missing values to 0's

2009-01-14 Thread Jorge Ivan Velez
Dear rafamoral, Try this: x[is.na(x)]<-0 HTH, Jorge On Wed, Jan 14, 2009 at 5:32 PM, rafamoral wrote: > > I have a dataset which contains some missing values, and I need to replace > them with zeros. I tried using the following: > > x <- matrix(data=rep(c(1,2,3,NA),6), ncol=6, nrow=6) > > y <-

Re: [R] Logical function to turn missing values to 0's

2009-01-14 Thread Gustavo Carvalho
Hello rafamoral, Try this: ifelse(is.na(x),0,x) On Wed, Jan 14, 2009 at 8:32 PM, rafamoral wrote: > > I have a dataset which contains some missing values, and I need to replace > them with zeros. I tried using the following: > > x <- matrix(data=rep(c(1,2,3,NA),6), ncol=6, nrow=6) > > y <- matr