Re: [R] NA value

From: Doran, Harold <HDoran_at_air.org>
Date: Fri, 27 Jun 2008 11:14:51 -0400


Don't use a loop for this. Do this.

X <- matrix(c(NA, 2, NA, 4),ncol=2) # Sample matrix

X[is.na(X)] <- 0 # Do this instead

> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Alfredo
> Alessandrini
> Sent: Friday, June 27, 2008 10:37 AM
> To: r-help_at_r-project.org
> Subject: [R] NA value
>
> I'm trying to replace NA with 0 value...
>
> I've write a loop, but don't work...
>
> Where's the problem?
>
>
> > cimfasy_rwl
> 1991 0.92 0.72 0.50 1.29 0.54 1.22
> 1992 2.15 1.28 1.23 2.26 1.22 3.17
> 1993 1.50 0.87 1.68 1.97 0.83 2.55
> 1994 0.69 0.00 0.76 1.89 0.60 0.87
> 1995 1.13 1.04 1.19 1.52 1.13 1.78
> 1996 1.15 0.92 1.50 0.97 0.60 NA
> 1997 NA NA NA NA NA NA
> 1998 NA NA NA NA NA NA
> 1999 NA NA NA NA NA NA
> 2000 NA NA NA NA NA NA
> > files_rwl <- ls(pattern="_rwl$")
> > files_rwl
> [1] "cimfasy_rwl" "rocquce_rwl"

> > for (i in files_rwl) assign(i,i[is.na(i)] <- 0)
> > ls()
> [1] "cimfasy_rwl" "files_rwl" "files.rwl" "i"

> "rocquce_rwl"
> >
> > cimfasy_rwl
> [1] 0
> >
>
>
> Thanks in advance,
>
> Alfredo
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> 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.
>



R-help_at_r-project.org mailing list
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. Received on Fri 27 Jun 2008 - 15:17:43 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 27 Jun 2008 - 16:30:44 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive