[R] [External] Convert a character string to variable names

Erin Hodgess er|nm@hodge@@ @end|ng |rom gm@||@com
Tue Feb 8 01:05:26 CET 2022


Thanks to all!



On Mon, Feb 7, 2022 at 4:56 PM Bert Gunter <bgunter.4567 using gmail.com> wrote:

> Yes, I said that Rui. If she is really just trying to extract equal length
> columns from a data frame or list by names(as strings), as I said,  then
> none of this is necessary:
> as.matrix(mtcars[, x])  ## does it
>
> Or have I missed something?
>
> Bert
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Mon, Feb 7, 2022 at 3:36 PM Rui Barradas <ruipbarradas using sapo.pt> wrote:
>
> > Hello,
> >
> > But that doesn't work in the OP's case:
> >
> >
> > sapply(.x, get)
> > #Error in FUN(X[[i]], ...) : object 'mtcars$disp' not found
> >
> >
> > Like you say, as.name(x$something) is not the name of an object.
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> > Às 23:21 de 07/02/2022, Bert Gunter escreveu:
> > > I assume that the mtcars components were used only as an example, as
> you
> > > could convert them to a matrix via as.matrix(). Unfortunately, it was a
> > bad
> > > choice: as.name("x$something") is not the name of 'something' in the
> > data
> > > frame (or list) x. Moreover, as.name("x") does not point to an object
> x
> > --
> > > as Rich indicated, it has to be evaluated to do so.
> > >> x<- 1:10
> > >> z <- as.name("x")
> > >> z
> > > x
> > >> length(z)
> > > [1] 1
> > >> eval(z)
> > >   [1]  1  2  3  4  5  6  7  8  9 10
> > >
> > > But more to the point, I think you are attempting to drive from
> Brooklyn
> > to
> > > New York City by way of Los Angeles. If I understand correctly, I think
> > all
> > > you need is ?get:
> > >
> > >> x <- runif(5)
> > >> y <- 1:5
> > >> z <- 21:25
> > >> nm <- c('x', 'y','z')
> > >> sapply(nm, get)
> > >                x y  z
> > > [1,] 0.99602479 1 21
> > > [2,] 0.97458756 2 22
> > > [3,] 0.09496625 3 23
> > > [4,] 0.91444550 4 24
> > > [5,] 0.68331484 5 25
> > >
> > > Cheers,
> > > Bert Gunter
> > >
> > > "The trouble with having an open mind is that people keep coming along
> > and
> > > sticking things into it."
> > > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> > >
> > >
> > > On Mon, Feb 7, 2022 at 3:06 PM Richard M. Heiberger <rmh using temple.edu>
> > wrote:
> > >
> > >>> x <- c("mtcars$disp", "mtcars$hp", "mtcars$cyl")
> > >>> x
> > >> [1] "mtcars$disp" "mtcars$hp"   "mtcars$cyl"
> > >>> eval(parse(text=x))
> > >>   [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
> > >>> for (i in x) print(eval(parse(text=i)))
> > >>   [1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6
> 167.6
> > >> 275.8 275.8 275.8 472.0 460.0 440.0  78.7  75.7  71.1 120.1 318.0
> > >> [23] 304.0 350.0 400.0  79.0 120.3  95.1 351.0 145.0 301.0 121.0
> > >>   [1] 110 110  93 110 175 105 245  62  95 123 123 180 180 180 205 215
> > 230
> > >> 66  52  65  97 150 150 245 175  66  91 113 264 175 335 109
> > >>   [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
> > >>
> > >>
> > >>> On Feb 07, 2022, at 17:55, Erin Hodgess <erinm.hodgess using gmail.com>
> > wrote:
> > >>>
> > >>>> .x
> > >>>
> > >>> [1] "mtcars$disp" "mtcars$hp"   "mtcars$cyl"
> > >>
> > >> ______________________________________________
> > >> R-help using r-project.org 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.
> > >>
> > >
> > >       [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help using r-project.org 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.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org 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.
>
-- 
Erin Hodgess, PhD
mailto: erinm.hodgess using gmail.com

	[[alternative HTML version deleted]]



More information about the R-help mailing list