[R] Obtaining variable's names from a list of variables
Eik Vettorazzi
E.Vettorazzi at uke.uni-hamburg.de
Wed Aug 17 14:45:42 CEST 2011
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
More information about the R-help
mailing list