System info: Linux slackware R Version 1.5.1 (2002-06-17) ESS 5.1.21 Emacs 21.2.1 ------------------- Colleagues I have some text files in a certain directory and want to process them sequentially. So I want to pass the filename identifier into a function and loop through the processing of each file in turn.I've called the file identifier f in this code segment: image.dvm <- function (horiz.slice.int=2, ping.int=3, start.h=3, start.min=4, max.z=800, f=350:372) { ## Purpose: to display the whole echogram integrals for consecutive CREST ## files to illustrate patterns of diel vertical migration ## ---------------------------------------------------------------------- ## Arguments: ## ---------------------------------------------------------------------- ## Author: Sam McClatchie, Date: 6 Sep 2002, 12:02 for (i in 1:length(f)) paste("t.",f[1], sep="") <-read.table(paste("/home/smc/manuscripts/mesopelagic_fish/biomass_spectrum/we0000",f[1],sep=""), header=F) ----------------- There is nothing wrong with the text files because I can read them correctly with: > t.350 <- read.table('we0000350', header=F) > t.350[1:5,1:4] V1 V2 V3 V4 1 0.00000e+00 0.0000e+00 0.0000e+00 0.00000e+00 2 0.00000e+00 0.0000e+00 0.0000e+00 0.00000e+00 3 0.00000e+00 0.0000e+00 0.0000e+00 0.00000e+00 4 0.00000e+00 0.0000e+00 0.0000e+00 0.00000e+00 5 8.71053e-05 4.2294e-05 3.6614e-05 6.68651e-05 > However, running the code segment above gives an error that has me a little bemused: > image.dvm() Error in image.dvm() : Target of assignment expands to non-language object > I guess I'm doing something illegal with the paste command? paste("t.",f[1], sep="") <- read.table.... Can you help, please? Sam -- Sam McClatchie, Research scientist (fisheries acoustics)))))))))) NIWA (National Institute of Water & Atmospheric Research Ltd) PO Box 14 901, Kilbirnie, Wellington, New Zealand s.mcclatchie at niwa.cri.nz Research home page <http://www.smcc.150m.com/> /\ >><xX(&> /// \\\ //// \\\\ /// <%)Xx><< ///// \\\\\\ ><(((@> ><(((%> ..>><xX(?>O<?)Xx><< -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
See ?assign /Henrik Bengtsson> -----Original Message----- > From: owner-r-help at stat.math.ethz.ch > [mailto:owner-r-help at stat.math.ethz.ch]On Behalf Of Sam McClatchie > Sent: Monday, September 09, 2002 5:47 AM > To: r-help at stat.math.ethz.ch > Subject: [R] loading data from a specified directory > > > System info: > Linux slackware > R Version 1.5.1 (2002-06-17) > ESS 5.1.21 > Emacs 21.2.1 > ------------------- > Colleagues > > I have some text files in a certain directory and want to process them > sequentially. So I want to pass the filename identifier into a function > and loop through the processing of each file in turn.I've called the > file identifier f in this code segment: > > image.dvm <- function (horiz.slice.int=2, ping.int=3, > start.h=3, start.min=4, > max.z=800, f=350:372) > { > ## Purpose: to display the whole echogram integrals for > consecutive CREST > ## files to illustrate patterns of diel vertical migration > ## > ---------------------------------------------------------------------- > ## Arguments: > ## > ---------------------------------------------------------------------- > ## Author: Sam McClatchie, Date: 6 Sep 2002, 12:02 > > > for (i in 1:length(f)) > paste("t.",f[1], sep="") > <-read.table(paste("/home/smc/manuscripts/mesopelagic_fish/biomass > _spectrum/we0000",f[1],sep=""), > header=F) > > ----------------- > There is nothing wrong with the text files because I can read them > correctly with: > > t.350 <- read.table('we0000350', header=F) > > t.350[1:5,1:4] > V1 V2 V3 V4 > 1 0.00000e+00 0.0000e+00 0.0000e+00 0.00000e+00 > 2 0.00000e+00 0.0000e+00 0.0000e+00 0.00000e+00 > 3 0.00000e+00 0.0000e+00 0.0000e+00 0.00000e+00 > 4 0.00000e+00 0.0000e+00 0.0000e+00 0.00000e+00 > 5 8.71053e-05 4.2294e-05 3.6614e-05 6.68651e-05 > > > However, running the code segment above gives an error that has me a > little bemused: > > > image.dvm() > Error in image.dvm() : Target of assignment expands to non-language object > > > > I guess I'm doing something illegal with the paste command? > > paste("t.",f[1], sep="") <- read.table.... > > Can you help, please? > > Sam > -- > Sam McClatchie, Research scientist (fisheries acoustics)))))))))) > NIWA (National Institute of Water & Atmospheric Research Ltd) > PO Box 14 901, Kilbirnie, Wellington, New Zealand > s.mcclatchie at niwa.cri.nz > Research home page <http://www.smcc.150m.com/> > /\ > >><xX(&> > /// \\\ > //// \\\\ > /// <%)Xx><< > ///// \\\\\\ > ><(((@> > ><(((%> ..>><xX(?>O<?)Xx><< > > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._._._._._._ > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 Mon, 9 Sep 2002, Sam McClatchie wrote: | |I have some text files in a certain directory and want to process them |sequentially. So I want to pass the filename identifier into a function |and loop through the processing of each file in turn.I've called the |file identifier f in this code segment: | |image.dvm <- function (horiz.slice.int=2, ping.int=3, | start.h=3, start.min=4, | max.z=800, f=350:372) |{ | for (i in 1:length(f)) | paste("t.",f[1], sep="") |<-read.table(paste("/home/smc/manuscripts/mesopelagic_fish/biomass_spectrum/we0000",f[1],sep=""), |header=F) |Error in image.dvm() : Target of assignment expands to non-language object | > | |I guess I'm doing something illegal with the paste command? | | paste("t.",f[1], sep="") <- read.table.... Hi, You must not use paste() in this context. Paste cannot be used for assigning variable name, you must use assign() instead. Write something like assign(paste("t.", f[1], sep=""), read.table(...)) but check it. Cheers, Ott -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 Mon, 9 Sep 2002, Sam McClatchie wrote:> for (i in 1:length(f)) > paste("t.",f[1], sep="") > <-read.table(paste("/home/smc/manuscripts/mesopelagic_fish/biomass_spectrum/we0000",f[1],sep=""), > header=F)As has been pointed out, this isn't a problem with loading data from a specified directory. If you want to assign to a constructed variable name you can do it with either assign() or by constructing an expression with substitute() and evaluating it. However, you typically don't want to do that -- it's usually easier to stick all the things in a list. datasets<-vector("list",length(f)) for (fi in f){ datasets[[fi]]<-read.table(paste("/long/directory/name/",fi,sep="")) } -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._