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. thanks Carl
Henrique Dallazuanna
2009-Jun-18 21:33 UTC
[R] How to parse and eval a collection of items
You can get the values using get: sapply(ls(patt = "foo"), get) On Thu, Jun 18, 2009 at 6:25 PM, Carl Witthoft <carl@witthoft.com> 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. > > thanks > Carl > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
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? cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
This does not use an apply: unlist(mget(thels, .GlobalEnv)) On Thu, Jun 18, 2009 at 5:25 PM, Carl Witthoft<carl at witthoft.com> 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. > > thanks > Carl > > ______________________________________________ > 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. >