Say I have a list of variables, listVar <- list(age,sex) I am looking for a way to either 1- create a vector c("age","sex") from it, or 2- get the names one by one in a for loop such as these a) for (i in 1:length(listVar)) rownames(result)[i] <- ??? b) for(i in listVar) print (variable's name) Any help much appreciated. [[alternative HTML version deleted]]
Eik Vettorazzi
2011-Aug-17 12:45 UTC
[R] Obtaining variable's names from a list of variables
Hi, there is no direct way, since listVar <- list(age,sex) creates a unnamed list, as can be seen by names(listVar) #or str(listVar) You can do sth like listVar <- list(age=age,sex=sex) # or listVar2 <- list(age,sex) names(listVar2)<-c("age","sex") and afterwards access them using names(). Or you write your own list function using its call to name the returned object, as in my.list<-function(...){ tmp<-list(...) names(tmp)<-all.names(match.call())[-1] tmp } attach(iris) a<-my.list(Sepal.Length,Sepal.Width) hth. Am 17.08.2011 08:46, schrieb Monsieur Do:> Say I have a list of variables, > > listVar <- list(age,sex) > > I am looking for a way to either > > 1- create a vector c("age","sex") from it, or > 2- get the names one by one in a for loop such as these > > a) for (i in 1:length(listVar)) rownames(result)[i] <- ??? > > b) for(i in listVar) print (variable's name) > > > Any help much appreciated. > [[alternative HTML version deleted]] > > > > > ______________________________________________ > 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.-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790
Marc Schwartz
2011-Aug-17 12:48 UTC
[R] Obtaining variable's names from a list of variables
On Aug 17, 2011, at 1:46 AM, Monsieur Do wrote:> Say I have a list of variables, > > listVar <- list(age,sex) > > I am looking for a way to either > > 1- create a vector c("age","sex") from it, or > 2- get the names one by one in a for loop such as these > > a) for (i in 1:length(listVar)) rownames(result)[i] <- ??? > > b) for(i in listVar) print (variable's name) > > > Any help much appreciated.Based upon the way in which you created 'listVar', there really is not a way to recover the variable names, since they are not retained: age <- 1:2 sex <- c("M", "F") listVar <- list(age, sex)> listVar[[1]] [1] 1 2 [[2]] [1] "M" "F"> names(listVar)NULL On the other hand, if you use: listVar <- list(age = age, sex = sex)> listVar$age [1] 1 2 $sex [1] "M" "F"> names(listVar)[1] "age" "sex" HTH, Marc Schwartz
Thank you for your answers. Problem solved. Eik's cue to all.names(match.call())[-1]?was particularly enlightning! Do ?> De?: Eik Vettorazzi[mailto:E.Vettorazzi at uke.uni-hamburg.de]> Envoy??: 17 ao?t 2011 08:46 > ??: Monsieur Do > Cc?: r-help at r-project.org > Objet?: Re: [R] Obtaining variable's namesfrom a list of variables> > Hi, > there is nodirect way, since> listVar<- list(age,sex)> creates aunnamed list, as can be seen by> names(listVar)#or> str(listVar) > > You can dosth like> listVar<- list(age=age,sex=sex) # or> listVar2<- list(age,sex)> names(listVar2)<-c("age","sex") > > andafterwards access them using names().> > Or youwrite your own list function using its call to name the returned object,> as in > > my.list<-function(...){ > ?tmp<-list(...) > ?names(tmp)<-all.names(match.call())[-1] > ?tmp > } > attach(iris) > a<-my.list(Sepal.Length,Sepal.Width) > > hth. > > Am17.08.2011 08:46, schrieb Monsieur Do:> > Say Ihave a list of variables,> > > >listVar <- list(age,sex)> > > > I amlooking for a way to either> > > > 1-create a vector c("age","sex") from it, or> > 2- getthe names one by one in a for loop such as these> > > >???? a)? for (i in 1:length(listVar)) rownames(result)[i] <- ??? > > > >???? b)? for(i in listVar) print (variable's name) > > > > > > Anyhelp much appreciated.> > ??????? [[alternative HTML version deleted]] > > > > > > > > > >______________________________________________> > R-help at r-project.orgmailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASEdo read the posting guide> > http://www.R-project.org/posting-guide.html > > andprovide commented, minimal, self-contained, reproducible code.> > -- > EikVettorazzi> Institutf?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum> Hamburg-Eppendorf > > Martinistr.52> 20246Hamburg> > T++49/40/7410-58243> F++49/40/7410-57790