drflxms
2011-Sep-13 08:11 UTC
[R] How to use a variable after $ ($x) for subscripting a list, How to source from a character object (source(x))
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. Michael Weylandt
2011-Sep-13 08:29 UTC
[R] How to use a variable after $ ($x) for subscripting a list, How to source from a character object (source(x))
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@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@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.
>
[[alternative HTML version deleted]]
David Winsemius
2011-Sep-13 13:15 UTC
[R] How to use a variable after $ ($x) for subscripting a list, How to source from a character object (source(x))
On Sep 13, 2011, at 4:11 AM, drflxms 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] >Naming the last dog "cc" instead of "c": > as.data.frame(lapply(listResult, "[", 2) ) a b cc 1 2 2 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.It would have been had you used this form:> foreach(i=1:length(levels)) %do% {listResult[[i]][[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.Correct. That is the main difference between "$" and "[[" and the reason "$" is far less useful in programming situations.> 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)I'm afraid you lost me after a while. If using "[[" with a character- valued argument which is intended to be interpreted is not what you needed, then make a slightly less wordy posting that has an example of what you wanted.> (3) How to avoid both ;)Avoid .... what?> > Unfortunately I am not experienced enough to find the solution. > Please help! > > Greetings from sunny Munich, Felix >-- David Winsemius, MD West Hartford, CT
Apparently Analagous Threads
- [LLVMdev] LLVM Source-to-Source transformation
- colouring a table, data.frame or matrix in an interactive R session
- percentile of a given value: is there a "reverse" quantile function?
- simple generation of artificial data with defined features
- how to sort the levels of a table