[R] How to use a variable after $ ($x) for subscripting a list, How to source from a character object (source(x))

drflxms drflxms at googlemail.com
Tue Sep 13 14:50:40 CEST 2011


Thank you very much!

foreach(i=1:length(levels)) %do% {listResult[[i]][[input]]} (instead of
listResult[[i]]$input) does the trick!!!

Sorry for being a little bit confuse in describing the problem.
My example object listResult is meant to be a list-object with several
levels. More concrete: it contains the output of confusionMatrix
function from library(caret) as well as other output for several
confusion matrices.
Hope, this is a little bit clearer now?

Thank you very much for your examples, which help me to understand how
to program similar problems.

Thanx and all the best, happy Felix

Am 13.09.11 10:29, schrieb R. Michael Weylandt:
> I had a little bit of trouble following what exactly you mean to do
> (specifically, I'm having a little bit of trouble understanding your sample
> object "listResult" -- is it a list or a set of factors or a list of lists
> or something else entirely), but I believe that replacing the `$` operator
> with additional copies of  `[` or `[[` as appropriate should get the job
> done. $ is, for almost all purposes, a less flexible version of `[[`.
> 
> Consider the following:
> 
> R> a = list(a1 = rnorm(5), rnorm(6), rnorm(7))
> R> b = list(runif(5), b2 = runif(6), runif(7))
> R> c = list(-runif(5), -runif(6), c3 = -runif(7))
> 
> R> A = list(a = a,b = b,c = c)
> 
> R> for( i in seq_along(A)) { print(A[[i]][[3]][i]) }
> 
> which seems to work for me (even if it is frustratingly opaque as to its
> function). Specifically,
> 
> R> identical(A$a, A[["a"]])
> TRUE
> 
> R> identical(A$a,A[[1]])
> TRUE
> 
> If this doesn't work, please clarify what exactly the object you have is and
> what you are trying to get out of it and I'll give a more concrete answer.
> 
> On Tue, Sep 13, 2011 at 4:11 AM, drflxms <drflxms at googlemail.com> wrote:
> 
>> Dear R colleagues,
>>
>> as result of a function a huge list is generated. From this result list
>> I'd like to extract information.
>>
>> Think of the list i.e. as an object named "listResult" with the
>> following form:
>>
>> [[a]]
>>  [1] [2] [3] [4] [5]
>>
>> [[b]]
>>  [1] [2] [3] [4] [5]
>>
>> [[c]]
>>  [1] [2] [3] [4] [5]
>>
>> where levels=c(a,b,c)
>>
>> I'd like to extract a data.frame like
>>
>> a [2]
>> b [2]
>> c [2]
>>
>> What I tried, is a function like this:
>>
>> library(foreach)
>> loopExtract <- function(input) {
>> foreach(i=1:length(levels)) %do% {listResult[[i]]$input}
>> ...
>>
>> where the name of the variable [2] is meant to be the input.
>>
>> Unfortunately it turned out, that the "input"-variable after the $ is
>> not substituted as expected. Subscripting the list with a variable after
>> the $ seems not possible.
>> The only workaround I found for that is a function like
>>
>> loopExtract <- function(input) {
>> codetemplate <- as.character("result <- foreach(i=1:length(levels)) %do%
>> {listResult[[i]]$input)}")
>> require(stringr)
>> write(str_replace_all(codetemplate, "input", input), file="tmp.r")
>> source("tmp.r")
>> return(result)
>> }
>>
>> in other words I stored a template of the desired code as characters in
>> an object, substituted the "input" string in that character object,
>> wrote the result to a file and sourced the code of that file.
>>
>> I stored the result in a file cause the expression source(codetemplate)
>> did not work. From the documentation I learned, that one can only source
>> "connections". And there seems no chance to establish a connection to an
>> object. (Probably no one else, except me, has such a strange idea.)
>>
>> Well, it works that way, but even though I am not a real programmer, I
>> realize how dirty this solution is. There must be a much simpler and
>> elegant way.
>>
>> To sum it all up, my problem can be reduced to the following two questions
>>
>> (1) How to subscript a list with a variable after $
>> (2) How to source from an object containing a code template
>> or (probably the most promising) (3) How to avoid both ;)
>>
>> Unfortunately I am not experienced enough to find the solution. Please
>> help!
>>
>> Greetings from sunny Munich, Felix
>>
>> ______________________________________________
>> 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.
>>
>



More information about the R-help mailing list