[R] Change NA value into 0

smart hendsome putra_autumn86 at yahoo.com
Tue Mar 1 07:39:17 CET 2016


Hi R-users,
I have problem regarding my function. My function as below:
gen.m <- function(n,itr){

    set.seed(1234)
    
    m <- matrix(nrow = n, ncol=4)
       a <- matrix(nrow = n, ncol = 1) 
       b <- matrix(nrow = n, ncol = 1) 
       C <- matrix(nrow = n, ncol = 1) 
       d <- matrix(nrow = n, ncol = 1) 
  lambda <- 0.342
    day0 <- 0.1134
   
      x = matrix(runif(n*itr, 0, 1),nrow = n, ncol = itr)

  y = 0
  for(i in 1:n){
    a[i] = x[i,1]
    b[i] = y - (log (a[i])) / lambda
       y =  b[i]
    }

  if (a[1] < day0){
    C[1] = 0
    }else{
    C[1] = 1}
    
    for (i in 1:n){
       w = as.integer(b[i])
       if (w <= n){
         C[w] = 1
         }else{
         C[w] = 0}
     }
    
  
    for (i in 2:n)
       rain2 <- cbind(a,b,C,d)
       y <- cbind(a,b,C)
       return(y)
   }


  gen.m(31,10)

When i run my output, it gave the result NA. I want the NA values is zero. Hope anyone can help me. Thanks so much.

	[[alternative HTML version deleted]]



More information about the R-help mailing list