Dear kxk, Here are a couple of options: # Option 1 -- sapply mydata <- sapply(1970:2005, function(i){ mydata <- read.table(file = paste("edge_", i, ".csv", sep=""), header=TRUE) # more code goes here } )
Please note that mydata would be a list, so to access the data for 1970, type mydata[[1]] in the R Console. # Option 2 -- create a function foo Another possibility would be to create a function, e.g foo, that reads the data in and does the procedures you need for each file. It would be something like: foo <- function(nameFile){ mydata <- read.table(file = paste("edge_", nameFile, ".csv", sep=""), header=T) # more code goes here } To do what you want for one file, just type onefile <- foo(1970) in the R Console. To do all your files at once, then myresults <- sapply(1970:2005, foo) is what you need. Once again, myresults would be a list. Now, If you insist using a for loop, then 1. Avoid to use "t". It is an R function. See ?t 2. Rewrite your loop as edge <- NULL for (i in 1970:2005){ edge[[i]] <- read.table(file = paste("edge_", i, ".csv", sep=""), header=TRUE) # more code goes here } 3. Use the "[[" operator to extract the information you need from "edge". Also, you might be interested in the foreach package < http://cran.r-project.org/web/packages/foreach/>. Take a look at the Vignette that come with it. See ?sapply and ?"for" for more details. HTH, Jorge On Wed, Jul 22, 2009 at 5:46 PM, kxk <kk...@u.washington.edu> wrote: > > I want to use read.table to input many files, each for a different year. I > would like to use the macro variable 't' to refer to the exact file that I > would like to input the data using read.table. How could I do this? Thank > you! > > for (t in 1970:2005) > { edge <- read.table(file="edge_t.csv", header=T, sep=",") > ## I will have many rows of code following the read.table line > } > > -- > View this message in context: > http://www.nabble.com/How-to-use-macro-variable-in-a-text-string-tp24615030p24615030.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.