Use the index number on the variable. Remember that a data
frame is just like a list, and you have a list of data frames.
Let's call the list of data frames "datas":
results <- list()
for (j in 1:length(variables)) {
for (i in 2:length(groups)) {
var.across.data <- datas[[1]][[j]]
var.across.data <- c(var.across.data, datas[[i]][[j]])
}
results[[j]] <- do.stuff(var.across.data)
}
Note the order of the nested loop. You want to do something
across data frames, by variable, so the variables go on the
outside loop.
----- Original Message -----
From: "Allan Friedman" <allan.friedman(a)gmail.com>
To: <gov2001-l(a)lists.fas.harvard.edu>
Sent: Sunday, December 19, 2004 1:35 PM
Subject: [gov2001-l] dynamic variable names in R?
Suppose I have several dataframes: g1, g2, g3....
Each as the same variable names: X, Y....
I want to write a function that can deal with variables across
the
dataframes. The easiest way that I can think of to do it
would be to
write a function that could walk through each dataframe and do
stuff
to the variables, something like:
mush.data <- function(varname, numgroups) {
for(i in 1:numgroups) {
SomeFunction(paste("g", i, "$",varname)
}
BUT - paste just gives me a string. How do I get R to realize
that
it's actually pointing to a vector? I feel that R should let
me do
this, right?
Any help would be greatly appreciated.
/\llan
_______________________________________________
gov2001-l mailing list
gov2001-l(a)lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l