values of list of variable names

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

values of list of variable names

Christian Hoffmann-3
Hi,

I have searched the documentations of eval, substitute, expression, and
I cannot make work something like the values of a list of variable names:

lis <- ls(pattern="pr") # all variables with names containing 'pr'

What is the mantra giving me the _values_ of the variables whose names
are  contained in 'lis'. eval(parse(ls(pattern="pr"))) will not do but
returning TRUE.

TIA
C.
--
Christian Hoffmann
Rigiblickstrasse 15b
CH-8915 Hausen am Albis
Switzerland
Telefon +41-(0)44-7640853

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: values of list of variable names

R help mailing list-2
One way is
   values <- lapply(lis, get)
You can do
   names(values) <- lis
to attach the object names to the values in the list returned by lapply.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Jun 1, 2018 at 7:25 AM, Christian <[hidden email]> wrote:

> Hi,
>
> I have searched the documentations of eval, substitute, expression, and I
> cannot make work something like the values of a list of variable names:
>
> lis <- ls(pattern="pr") # all variables with names containing 'pr'
>
> What is the mantra giving me the _values_ of the variables whose names
> are  contained in 'lis'. eval(parse(ls(pattern="pr"))) will not do but
> returning TRUE.
>
> TIA
> C.
> --
> Christian Hoffmann
> Rigiblickstrasse 15b
> CH-8915 Hausen am Albis
> Switzerland
> Telefon +41-(0)44-7640853
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: values of list of variable names

jholtman
In reply to this post by Christian Hoffmann-3
You probably want to use 'get':

> r1 <- 5
> r2 <- 3
> r3 <- 45
> x <- ls(pattern = '^r.$')
> x
[1] "r1" "r2" "r3"
> lapply(x, get)
[[1]]
[1] 5

[[2]]
[1] 3

[[3]]
[1] 45

>


Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Fri, Jun 1, 2018 at 7:25 AM, Christian <[hidden email]> wrote:

> Hi,
>
> I have searched the documentations of eval, substitute, expression, and I
> cannot make work something like the values of a list of variable names:
>
> lis <- ls(pattern="pr") # all variables with names containing 'pr'
>
> What is the mantra giving me the _values_ of the variables whose names
> are  contained in 'lis'. eval(parse(ls(pattern="pr"))) will not do but
> returning TRUE.
>
> TIA
> C.
> --
> Christian Hoffmann
> Rigiblickstrasse 15b
> CH-8915 Hausen am Albis
> Switzerland
> Telefon +41-(0)44-7640853
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: values of list of variable names

Greg Minshall-2
In reply to this post by Christian Hoffmann-3
Christian,

does this do it?

> eval(lapply(ls(pattern="pr"), function(x) eval(parse(text=x))))

cheers, Greg

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.