[R] How to parse and eval a collection of items

Carl Witthoft carl at witthoft.com
Thu Jun 18 23:53:59 CEST 2009


Hi,

do.call doesn't help, but the solution provided by  Henrique D.  is much 
cleaner than my hack:

 > sapply(ls(patt='foo'),get)


Thanks to all.
Carl


Rolf Turner wrote:
> 
> On 19/06/2009, at 9:25 AM, Carl Witthoft wrote:
> 
>> Let's say I have, for some reason, a bunch of scalars (i.e.
>> single-valued variables) and I want to merge them all into a single
>> vector of values.  Can someone recommend a better function, or simpler
>> way, to do so than the following?
>>
>> Suppose my scalars' names are foo1, foo2, foo3, foo1high, foo2high,
>> foo3lo2, etc.  Then I can do:
>>
>>
>>
>>> ls(pat='foo')->thels
>>
>>
>>> mapply(function(x) eval(as.name(thels[x])),seq(1:length(thels)))->vecfoo
>>
>>
>> I was hoping for some way to avoid creating a function and an *apply
>> call.  Conceptually, eval(as.name(c(thels)))  which of course doesn't 
>> work.
> 
> Does
> 
>     do.call(c,thels)
> 
> work?
>




More information about the R-help mailing list