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 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