Greetings all. Any help with the following would be appreciated.
I want to create a data frame for each file in a directory. The following
code does not work but it may show what I am trying to do:
carmakes <-
c('BMW','Chrysler','Citroen','Fiat','Ford','Holden','Honda',
'Mercedes','MG','Mitsubishi','Nissan','Peugeot','Renault','Subaru','Toyota',
'VW')
for (brand in carmakes) {
fyle <-
paste("c:/data/cars03/",brand,".txt",sep="")
brand <- read.table(fyle, header = TRUE, sep = "\t",na.strings =
c("-","POA"),
colClasses=c("character",rep("numeric",7)),
comment.char = "#")
}
I need something like an unquote() function that will allow the
"brand" on
the LHS of the assignment to be treated as an object name instead of a
character string.
Murray
Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html
Department of Statistics, University of Waikato, Hamilton, New Zealand
Email: maj at waikato.ac.nz Fax 7 838 4155
Phone +64 7 838 4773 wk +64 7 849 6486 home Mobile 021 1395 862
How about
for( ) {
assign(brand, read.table(......))
}
?
-roger
Murray Jorgensen wrote:> Greetings all. Any help with the following would be appreciated.
>
> I want to create a data frame for each file in a directory. The following
> code does not work but it may show what I am trying to do:
>
> carmakes <-
c('BMW','Chrysler','Citroen','Fiat','Ford','Holden','Honda',
>
'Mercedes','MG','Mitsubishi','Nissan','Peugeot','Renault','Subaru','Toyota',
> 'VW')
> for (brand in carmakes) {
> fyle <-
paste("c:/data/cars03/",brand,".txt",sep="")
> brand <- read.table(fyle, header = TRUE, sep =
"\t",na.strings =
> c("-","POA"),
colClasses=c("character",rep("numeric",7)),
> comment.char = "#")
> }
>
> I need something like an unquote() function that will allow the
"brand" on
> the LHS of the assignment to be treated as an object name instead of a
> character string.
>
> Murray
>
>
>
>
>
>
> Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html
> Department of Statistics, University of Waikato, Hamilton, New Zealand
> Email: maj at waikato.ac.nz Fax 7 838 4155
> Phone +64 7 838 4773 wk +64 7 849 6486 home Mobile 021 1395 862
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
Murray Jorgensen <maj at stats.waikato.ac.nz> writes:> Greetings all. Any help with the following would be appreciated. > > I want to create a data frame for each file in a directory. The following > code does not work but it may show what I am trying to do: > > carmakes <- c('BMW','Chrysler','Citroen','Fiat','Ford','Holden','Honda', > 'Mercedes','MG','Mitsubishi','Nissan','Peugeot','Renault','Subaru','Toyota', > 'VW') > for (brand in carmakes) { > fyle <- paste("c:/data/cars03/",brand,".txt",sep="") > brand <- read.table(fyle, header = TRUE, sep = "\t",na.strings = > c("-","POA"), colClasses=c("character",rep("numeric",7)), > comment.char = "#") > } > > I need something like an unquote() function that will allow the "brand" on > the LHS of the assignment to be treated as an object name instead of a > character string. > > Murrayassign(brand, read.table(blablabla)) (Overlooked by many, but I would have expected that you'd been around for long enough to have noticed it....) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Try assign - a la the subject heading.
I think this should work.
carmakes <-
c('BMW','Chrysler','Citroen','Fiat','Ford','Holden','Honda',
'Mercedes','MG','Mitsubishi','Nissan','Peugeot','Renault','Subaru','Toyo
ta','VW')
for (i in 1:length(carmakes)) {
# Create your path and file name
fyle <-
paste("c:/data/cars03/",carmakes[i],".txt",sep="")
# Read fyle into a temp variable - replace everyhting after
# <- with your read.table command.
temp <- data.frame(x = runif(10), y = runif(10), z = runif(10))
# Use assign to create the object
assign(carmakes[i], temp)
}
HTH,
Andy
Thanks to Andy, Peter and Roger for drawing my attention to assign(), which is just what I needed and works fine. Murray At 14:11 30/12/2003 +1300, Murray Jorgensen wrote:>Greetings all. Any help with the following would be appreciated. > >I want to create a data frame for each file in a directory. The following >code does not work but it may show what I am trying to do: > >carmakes <- c('BMW','Chrysler','Citroen','Fiat','Ford','Holden','Honda', >'Mercedes','MG','Mitsubishi','Nissan','Peugeot','Renault','Subaru','Toyota', >'VW') >for (brand in carmakes) { > fyle <- paste("c:/data/cars03/",brand,".txt",sep="") > brand <- read.table(fyle, header = TRUE, sep = "\t",na.strings = > c("-","POA"), colClasses=c("character",rep("numeric",7)), > comment.char = "#") >} > >I need something like an unquote() function that will allow the "brand" on >the LHS of the assignment to be treated as an object name instead of a >character string. > >Murray > > > > > > >Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html >Department of Statistics, University of Waikato, Hamilton, New Zealand >Email: maj at waikato.ac.nz Fax 7 838 4155 >Phone +64 7 838 4773 wk +64 7 849 6486 home Mobile 021 1395 862 > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: maj at waikato.ac.nz Fax 7 838 4155 Phone +64 7 838 4773 wk +64 7 849 6486 home Mobile 021 1395 862