[R] Odp: a loop....

Petr PIKAL petr.pikal at precheza.cz
Wed Jun 25 12:20:27 CEST 2008


Hi

r-help-bounces at r-project.org napsal dne 25.06.2008 10:24:35:

> I'm trying to make a loopo with some file....
> 
> >
> > library(dplR)
> > files.rwl <- system("ls *.rwl", intern=TRUE) > files.rwl
> [1] "cimfasy.rwl" "rocquce.rwl"
> > for (i in files.rwl) assign(gsub("\\.rwl$", "_rwl", i), read.rwl(i, 
header=0))
> There are 70 series
> There are 21 series
> >
> > cimfasy_rwl
> 
> 1990     1.55     0.00     1.59     1.94     0.99     2.14
> 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
> >
> 
> OK.... the loop work...
> 
> Now I want to replace the NA with 0 in the data.frame (cimfasy_rwl,
> rocquce_rwl, ecc...)
> 
> 
> > files_rwl <- ls(pattern="_rwl$")
> > files_rwl
> [1] "cimfasy_rwl" "rocquce_rwl"
> > for (i in files_rwl) assign(gsub("\\_rwl$", "", i), i[is.na(i) ] <- 0)
> > ls()
> [1] "cimfasy"     "cimfasy_rwl" "files_rwl"   "files.rwl"   "i"
> [6] "rocquce"     "rocquce_rwl"
> 
> But the loop don't work:

You probably mixup cimfasy and cimfasy_rwl which are different objects

I recommend avoid loop in this case

> zdrz[2,2]<-NA
> zdrz
    doba otac sklon
1  189.0  0.6   110
2  256.0   NA    80
3  286.0  0.6    50
4  105.0  1.2    50
> zdrz[is.na(zdrz)]
[1] NA
> zdrz[is.na(zdrz)]<-0

> zdrz
    doba otac sklon
1  189.0  0.6   110
2  256.0  0.0    80
3  286.0  0.6    50
4  105.0  1.2    50
5  272.0  1.2    10
6

And you shall know that R is pretty smart in handling missing values. How 
do you know after change NA -> 0 that 0 is from NA and not 
like in 1994 row and second column.

Regards
Petr

> 
> > cimfasy
> [1] 0
> 
> Why??
> 
> 
> 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.



More information about the R-help mailing list