Hi R users,
I am a newbie and therefore the naive question. Sorry but I was unable
to find an answer online. I would like to read 47 data sets
(country1.raw, country2.raw, ... country47.raw) and save into a matrix
each time (data1, data2, ....data47).
for (i in 1:47){
data(?) <- read.csv(file.path("country", i, ".raw", fsep
= ""), header = TRUE)
}
The read.cvs statement seems to be right, but how to include "i" in
"data" for it to create 47 matrices and not 1 (the last one).
Thank you,
Daniel
Consider using a 'list' to hold the matrices:
dataList <- lapply(1:47, function(num){
read.csv(paste('country', num, '.raw', sep=''),
header=TRUE)
}
You can then access your data like:
dataList[[23]] for the 23rd file read in.
On Tue, Jul 20, 2010 at 7:52 AM, Daniel Caro <dcarov at googlemail.com>
wrote:> Hi R users,
>
> I am a newbie and therefore the naive question. Sorry but I was unable
> to find an answer online. I would like to read 47 data sets
> (country1.raw, country2.raw, ... country47.raw) and save into a matrix
> each time (data1, data2, ....data47).
>
> for (i in 1:47){
> data(?) <- read.csv(file.path("country", i, ".raw",
fsep = ""), header = TRUE)
> }
>
> The read.cvs statement seems to be right, but how to include "i"
in
> "data" for it to create 47 matrices and not 1 (the last one).
>
> Thank you,
> Daniel
>
> ______________________________________________
> R-help at 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
Hi Daniel,
Try this:
for(i in 1:47) {
assign(x = paste("data", i, sep = ""),
value = read.csv(file.path("country", i, ".raw", fsep =
""), header = TRUE),
envir = .GlobalEnv)
}
See ?assign for documentation.
Cheers,
Josh
On Tue, Jul 20, 2010 at 4:52 AM, Daniel Caro <dcarov at googlemail.com>
wrote:> Hi R users,
>
> I am a newbie and therefore the naive question. Sorry but I was unable
> to find an answer online. I would like to read 47 data sets
> (country1.raw, country2.raw, ... country47.raw) and save into a matrix
> each time (data1, data2, ....data47).
>
> for (i in 1:47){
> data(?) <- read.csv(file.path("country", i, ".raw",
fsep = ""), header = TRUE)
> }
>
> The read.cvs statement seems to be right, but how to include "i"
in
> "data" for it to create 47 matrices and not 1 (the last one).
>
> Thank you,
> Daniel
>
> ______________________________________________
> R-help at 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.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/