[R] Change NA value into 0

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Mar 1 07:46:39 CET 2016


You do

   for(i in 1:n){
     a[i] = x[i,1]

where a has length n and x has iter rows, hence you get NA values for 
all i > iter...

Best,
Uwe Ligges



On 01.03.2016 07:39, smart hendsome via R-help wrote:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list