stefan.duke at gmail.com
2009-Jan-30 17:06 UTC
[R] paste together object names to pass it on to a function
Hello,
I have a maybe trivial question, but I simply don't understand well
enought how to work with text/strings:
I have a rather compelx data structure, a big list with several
sub-lists/dataframes and for certain calculations (which I do in
loops), I only need a certain group of sub-lists/dataframes, which I
want to specify with a name vector and paste together the object name
and pass it on to a function.
Here an (hopefully) instructive example
#Data Example
gnuff<-list()
gnuff$IHD$LE<-66
gnuff$LUNG$LE <-55
#This is the list, where I collect data for different diseases at the
second level of the list
#Now I want to do calcualtions just for these two diseases and the
sub-list "LE" within these diseases
nam <- c("LUNG","IHD")
for(i in 1:2)
x[i] <- paste("gnuff",nam[i],"LE",sep="$") /2
x
#So I try to paste the name of the object which I mean
(gnuff$IHD$LEand gnuff$LUNG$LE, respectivly), but R treats them as a
string and not as the name of an object.
# I tried seveal commands to make it treat like an object name (the
get() looked most promising), but so far to no avail
#commands I have tried
j <- eval(paste("gnuff",nam[i],"LE",sep="$"))
parse(paste("gnuff",nam[i],sep="$"))
quote(paste("gnuff",nam[i],sep="$"))
get(paste("gnuff",nam[i],sep="$"))
Anybody any hints where to look?
Thanks and have a great weekend!
Best,
Stefan
jim holtman
2009-Jan-30 17:15 UTC
[R] paste together object names to pass it on to a function
This should do it: ?"[["> gnuff<-list() > gnuff$IHD$LE<-66 > gnuff$LUNG$LE <-55 > > > > nam <- c("LUNG","IHD") > > x <- numeric(2) # allocate > for(i in 1:2)+ x[i] <- gnuff[[nam[i]]]$LE / 2> > x[1] 27.5 33.0 On Fri, Jan 30, 2009 at 12:06 PM, stefan.duke at gmail.com <stefan.duke at gmail.com> wrote:> Hello, > I have a maybe trivial question, but I simply don't understand well > enought how to work with text/strings: > > I have a rather compelx data structure, a big list with several > sub-lists/dataframes and for certain calculations (which I do in > loops), I only need a certain group of sub-lists/dataframes, which I > want to specify with a name vector and paste together the object name > and pass it on to a function. > > Here an (hopefully) instructive example > > #Data Example > gnuff<-list() > gnuff$IHD$LE<-66 > gnuff$LUNG$LE <-55 > > #This is the list, where I collect data for different diseases at the > second level of the list > #Now I want to do calcualtions just for these two diseases and the > sub-list "LE" within these diseases > > > nam <- c("LUNG","IHD") > > for(i in 1:2) > x[i] <- paste("gnuff",nam[i],"LE",sep="$") /2 > x > > #So I try to paste the name of the object which I mean > (gnuff$IHD$LEand gnuff$LUNG$LE, respectivly), but R treats them as a > string and not as the name of an object. > # I tried seveal commands to make it treat like an object name (the > get() looked most promising), but so far to no avail > > #commands I have tried > j <- eval(paste("gnuff",nam[i],"LE",sep="$")) > parse(paste("gnuff",nam[i],sep="$")) > quote(paste("gnuff",nam[i],sep="$")) > get(paste("gnuff",nam[i],sep="$")) > > Anybody any hints where to look? > Thanks and have a great weekend! > Best, > Stefan > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Patrick Burns
2009-Jan-30 17:35 UTC
[R] paste together object names to pass it on to a function
A rule of thumb is that if the solution seems a lot harder than the task, there is probably a better approach. I think you want something like: lapply(gnuff[nam], function(x) x$LE) Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of "The R Inferno" and "A Guide for the Unwilling S User") stefan.duke at gmail.com wrote:> Hello, > I have a maybe trivial question, but I simply don't understand well > enought how to work with text/strings: > > I have a rather compelx data structure, a big list with several > sub-lists/dataframes and for certain calculations (which I do in > loops), I only need a certain group of sub-lists/dataframes, which I > want to specify with a name vector and paste together the object name > and pass it on to a function. > > Here an (hopefully) instructive example > > #Data Example > gnuff<-list() > gnuff$IHD$LE<-66 > gnuff$LUNG$LE <-55 > > #This is the list, where I collect data for different diseases at the > second level of the list > #Now I want to do calcualtions just for these two diseases and the > sub-list "LE" within these diseases > > > nam <- c("LUNG","IHD") > > for(i in 1:2) > x[i] <- paste("gnuff",nam[i],"LE",sep="$") /2 > x > > #So I try to paste the name of the object which I mean > (gnuff$IHD$LEand gnuff$LUNG$LE, respectivly), but R treats them as a > string and not as the name of an object. > # I tried seveal commands to make it treat like an object name (the > get() looked most promising), but so far to no avail > > #commands I have tried > j <- eval(paste("gnuff",nam[i],"LE",sep="$")) > parse(paste("gnuff",nam[i],sep="$")) > quote(paste("gnuff",nam[i],sep="$")) > get(paste("gnuff",nam[i],sep="$")) > > Anybody any hints where to look? > Thanks and have a great weekend! > Best, > Stefan > > ______________________________________________ > 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. > > >
David Winsemius
2009-Jan-30 18:54 UTC
[R] paste together object names to pass it on to a function
Perhaps this will help:
#Data Example
gnuff<-list()
gnuff$IHD$LE<-66
gnuff$LUNG$LE <-55
#This is the list, where I collect data for different diseases at the
#second level of the list
#Now I want to do calcualtions just for these two diseases and the
#sub-list "LE" within these diseases
nam <- c("LUNG","IHD")
for(i in nam) print(gnuff[[i]]) # use the elements of nam as the
index values
# the lack of output from an evaluation done within the for-loop might
be one of Burns' Infernal examples.
# here's see one of my "mistakes": for(i in nam) (gnuff[[i]])
#---returns a list---
#$LE
#[1] 55
#$LE
#[1] 66
#---
for(i in nam) print(gnuff[[i]]$LE) #use list extraction to get the
values
#[1] 55
#[1] 66
On Jan 30, 2009, at 12:06 PM, stefan.duke at gmail.com wrote:
> Hello,
> I have a maybe trivial question, but I simply don't understand well
> enought how to work with text/strings:
>
> I have a rather compelx data structure, a big list with several
> sub-lists/dataframes and for certain calculations (which I do in
> loops), I only need a certain group of sub-lists/dataframes, which I
> want to specify with a name vector and paste together the object name
> and pass it on to a function.
>
> Here an (hopefully) instructive example
>
> #Data Example
> gnuff<-list()
> gnuff$IHD$LE<-66
> gnuff$LUNG$LE <-55
>
> #This is the list, where I collect data for different diseases at the
> second level of the list
> #Now I want to do calcualtions just for these two diseases and the
> sub-list "LE" within these diseases
>
>
> nam <- c("LUNG","IHD")
>
> for(i in 1:2)
> x[i] <- paste("gnuff",nam[i],"LE",sep="$")
/2
> x
>
> #So I try to paste the name of the object which I mean
> (gnuff$IHD$LEand gnuff$LUNG$LE, respectivly), but R treats them as a
> string and not as the name of an object.
> # I tried seveal commands to make it treat like an object name (the
> get() looked most promising), but so far to no avail
>
> #commands I have tried
> j <-
eval(paste("gnuff",nam[i],"LE",sep="$"))
> parse(paste("gnuff",nam[i],sep="$"))
> quote(paste("gnuff",nam[i],sep="$"))
> get(paste("gnuff",nam[i],sep="$"))
>
> Anybody any hints where to look?
> Thanks and have a great weekend!
> Best,
> Stefan
>
> ______________________________________________
> 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.