[R] assigning NA's
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Jan 22 18:52:45 CET 2002
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list