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 =
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
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 <-
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
4 matches
Mail list logo