Hi, I want to generate object names out of variables in a sort of variable substitution. first i generate some vectors and an empty list:> vector.a<-c("a","b") > vector.b<-c("c","d") > vector.c<-c("e","f") > vectors<-c("vector.a", "vector.b", "vector.c") > vectors[1] "vector.a" "vector.b" "vector.c"> vectorlist<-list()What I would then like to do is to generate elements of the list by using variables, somehow like this (does not work):>for (i in vectors) {+ list$i<-i + } To end up with a list like this:> list$vector.a [1] "a" "b" $vector.b [1] "c" "d" $vector.c [1] "e" "f" Any hint will be appreciated. Cheers, Georg
Try this: sapply(vectors, get, simplify = FALSE) or lapply(vectors, get) although the last one does not give you the names. On 7/14/06, Georg Otto <georg.otto at tuebingen.mpg.de> wrote:> Hi, > > I want to generate object names out of variables in a sort of variable > substitution. > > first i generate some vectors and an empty list: > > > vector.a<-c("a","b") > > vector.b<-c("c","d") > > vector.c<-c("e","f") > > vectors<-c("vector.a", "vector.b", "vector.c") > > vectors > [1] "vector.a" "vector.b" "vector.c" > > > vectorlist<-list() > > What I would then like to do is to generate elements of the list by > using variables, somehow like this (does not work): > > >for (i in vectors) { > + list$i<-i > + } > > To end up with a list like this: > > > list > $vector.a > [1] "a" "b" > $vector.b > [1] "c" "d" > $vector.c > [1] "e" "f" > > > Any hint will be appreciated. > > Cheers, > > Georg > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
On Fri, 2006-07-14 at 16:57 +0200, Georg Otto wrote:> Hi, > > I want to generate object names out of variables in a sort of variable > substitution. > > first i generate some vectors and an empty list: > > > vector.a<-c("a","b") > > vector.b<-c("c","d") > > vector.c<-c("e","f") > > vectors<-c("vector.a", "vector.b", "vector.c") > > vectors > [1] "vector.a" "vector.b" "vector.c" > > > vectorlist<-list() > > What I would then like to do is to generate elements of the list by > using variables, somehow like this (does not work): > > >for (i in vectors) { > + list$i<-i > + } > > To end up with a list like this: > > > list > $vector.a > [1] "a" "b" > $vector.b > [1] "c" "d" > $vector.c > [1] "e" "f" > > > Any hint will be appreciated. > > Cheers, > > GeorgPresuming that your vectors fit a naming pattern of "vector.x": # Use grep() to get the vector names from ls()> vectors <- grep("vector[\.]", ls(), value = TRUE)> vectors[1] "vector.a" "vector.b" "vector.c" # Use sapply to create the list> sapply(vectors, get, simplify = FALSE)$vector.a [1] "a" "b" $vector.b [1] "c" "d" $vector.c [1] "e" "f" HTH, Marc Schwartz
I collected some advice about this question a couple of years ago. This might help. http://pj.freefaculty.org/R/Rtips.html#2.1 "Add variables to a data frame (or list)" and the next one after that. On 7/14/06, Marc Schwartz (via MN) <mschwartz at mn.rr.com> wrote:> On Fri, 2006-07-14 at 16:57 +0200, Georg Otto wrote: > > Hi, > > > > I want to generate object names out of variables in a sort of variable > > substitution. > > > > first i generate some vectors and an empty list: > > > > > vector.a<-c("a","b") > > > vector.b<-c("c","d") > > > vector.c<-c("e","f") > > > vectors<-c("vector.a", "vector.b", "vector.c") > > > vectors > > [1] "vector.a" "vector.b" "vector.c" > > > > > vectorlist<-list() > > > > What I would then like to do is to generate elements of the list by > > using variables, somehow like this (does not work): > > > > >for (i in vectors) { > > + list$i<-i > > + } > > > > To end up with a list like this: > > > > > list > > $vector.a > > [1] "a" "b" > > $vector.b > > [1] "c" "d" > > $vector.c > > [1] "e" "f" > > > > > > Any hint will be appreciated. > > > > Cheers, > > > > Georg >-- Paul E. Johnson Professor, Political Science 1541 Lilac Lane, Room 504 University of Kansas