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.
Hi, On Jul 22, 2009, at 5:46 PM, kxk 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 > }Two things: 1. Not extremely important here at all, but for the future: just note that but by using "t" you're trampling over the transpose function t(), so perhaps you can use a more descriptive variable to both make code more readable and less ... urm, trample-itve :-) 2. Answer: for (year in 1970:2005) { edge <- read.table(file=sprintf("edge_%d.csv", year), header=T, sep=",") ... } You can also use the paste function in place of sprintf -steve -- Steve Lianoglou Graduate Student: Physiology, Biophysics and Systems Biology Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
On Jul 22, 2009, at 5:46 PM, kxk 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=",")Pretty sure that will not work. Take a look at the paste function for putting together strings and values. paste("test", 1:3, sep="") #[1] "test1" "test2" "test3" And you might consider whether you want "edge" to be wiped out the next time through the loop, too. That is what would happen with your current construction.> ## I will have many rows of code following the read.table line > } > > --David Winsemius, MD Heritage Laboratories West Hartford, CT
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 <kkong@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]]