I've had a question a few moments ago about how to create multiple objects from multiple files within a loop. Thanks for the quick answers, it worked with "assign", like this: for(i in seq(1,nfn,1)){ fin<-paste("/home/klimet/patrick/LAEGEREN/NEBEL/FOGEVENT2000/",fn[i],sep="") assign(paste("f", i, sep = ""), as.matrix(read.table(fin,skip=1))) } I've tried to use the same command for assigning NA's to missing values (=-999) in each object created with above loop, like this: for(i in seq(1,nfn,1)){ assign(paste("f", i, sep = "")[paste("f", i, sep = "") < -500.], NA) } This did not work, it created an error "invalid first argument" I've also tried: for(i in seq(1,nfn,1)){ paste("f", i, sep = "")[paste("f", i, sep = "") < -500.] <- NA } This also created an error. Any clues about how to handle this problem? Thanks again, Patrick -------------- next part -------------- A non-text attachment was scrubbed... Name: patrick.vcf Type: text/x-vcard Size: 339 bytes Desc: Card for Patrick Buetzberger Url : https://stat.ethz.ch/pipermail/r-help/attachments/20020122/4e7d77e2/patrick.vcf
On Tue, 22 Jan 2002, Patrick Buetzberger wrote:> I've had a question a few moments ago about how to create multiple > objects from multiple files within a loop. Thanks for the quick answers, > > it worked with "assign", like this: > > for(i in seq(1,nfn,1)){ > fin<-paste("/home/klimet/patrick/LAEGEREN/NEBEL/FOGEVENT2000/",fn[i],sep="") > > assign(paste("f", i, sep = ""), as.matrix(read.table(fin,skip=1))) > } > > I've tried to use the same command for assigning NA's to missing values > (=-999) in each object created with above loop, like this: > for(i in seq(1,nfn,1)){ > assign(paste("f", i, sep = "")[paste("f", i, sep = "") < -500.], NA) > } > > This did not work, it created an error "invalid first argument" > I've also tried: > for(i in seq(1,nfn,1)){ > paste("f", i, sep = "")[paste("f", i, sep = "") < -500.] <- NA > } > > > This also created an error. Any clues about how to handle this problem?You can only use character strings created by paste as names in assign, not as expressions. There are two ways to do this, and an extensive discussion in `S Programming' section 3.5. Perhaps the simpler is for(i in seq(1,nfn)) eval(parse(text = paste("f", i, "[f", i, " < -500] <- NA", sep = ""))) The other involves substitute(). This can very rapidly get pretty tricky, though. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 22 Jan 2002, Patrick Buetzberger wrote:> I've had a question a few moments ago about how to create multiple > objects from multiple files within a loop. Thanks for the quick answers, > > it worked with "assign", like this: > > for(i in seq(1,nfn,1)){ > fin<-paste("/home/klimet/patrick/LAEGEREN/NEBEL/FOGEVENT2000/",fn[i],sep="") > > assign(paste("f", i, sep = ""), as.matrix(read.table(fin,skip=1))) > } > > I've tried to use the same command for assigning NA's to missing values > (=-999) in each object created with above loop, like this: > for(i in seq(1,nfn,1)){ > assign(paste("f", i, sep = "")[paste("f", i, sep = "") < -500.], NA) > } > > This did not work, it created an error "invalid first argument"assign() doesn't dispatch things like f1[10]<-NA to the "[<-" methods, so this kind of approach won't work.> I've also tried: > for(i in seq(1,nfn,1)){ > paste("f", i, sep = "")[paste("f", i, sep = "") < -500.] <- NA > } > > This also created an error. Any clues about how to handle this problem?This kind of approach can work, but you need to create expressions rather than strings. expr<-substitute(fi[fi< -500]<-NA, list(fi=as.name(paste("f",i,sep="")))) creates expressions like f1[f1 < -500] <- NA and these need to be evaluated in the current environment eval(expr,envir=environment()) or possibly in the global environment eval(expr,envir=.GlobalEnv) Note that it would be much easier to create a single object (a list) rather than multiple objects, and it might well be easier to manipulate them that way afterwards. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> -----Original Message----- > From: Patrick Buetzberger [mailto:patrick at giub.unibe.ch] > Sent: Tuesday, January 22, 2002 12:36 PM > To: r-help at lists.R-project.org > Subject: [R] assigning NA's[...]> I've tried to use the same command for assigning NA's to > missing values > (=-999) in each object created with above loop, like this: > for(i in seq(1,nfn,1)){ > assign(paste("f", i, sep = "")[paste("f", i, sep = "") < -500.], NA) > }Probably the simplest solution is to use a temporary object inside your first loop and do the NA handling there. Something like: for(i in seq(1,nfn,1)){ fin<- paste("/home/klimet/patrick/LAEGEREN/NEBEL/FOGEVENT2000/",fn[i],sep="") dat<- as.matrix(read.table(fin,skip=1)) dat[dat< -500.] <- NA assign(paste("f", i, sep = ""), dat) } If you really need to keep the loops separate, you can use 'get' to grab the contents of each object, make the substitution, and then use 'assign' again to write it out: for(i in seq(1,nfn,1)){ dat<- get( paste("f", i, sep = "") ) dat[dat< -500.] <- NA assign(paste("f", i, sep = ""), dat) } -Greg LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._