[R] Convert a character string to variable names

Richard O'Keefe r@oknz @end|ng |rom gm@||@com
Fri Feb 11 06:10:15 CET 2022


Yes, in the expression mtcars$disp mtcars is a variable name
(and the value of that variable should be a list or data.frame
or something of the sort) and $disp is an indexing operation
that could also be written mtcars[["disp"]].

When you use mtcars$disp, you are necessarily using mtcars as
a variable.  The point was that there is no variable called
mtcars$disp.

The question remains: why are you doing this?  If you had
x <- c("mtcars", "trucks", "subs")
y <- c("disp", "load", "depth")
you could do
lapply(1:3, function (i) get(x[i])[[y[i]]])
in which get(x[2]) returns the data.frame that is the value
of trucks and trucks[["load"]] returns the $load column.
For that matter, you could have
x <- list(mtcars, trucks, subs)
lapply(1:3, function(i) x[i][[y[i]]])

I want to understand what the circumstances are that make
pasting the variable name and the column name together
more appropriate than keeping the two separate.

I want to understand whether the table name just *happened*
to be the same every time in your example or whether it
*had* to be the same.

What's actually going on here?


On Wed, 9 Feb 2022 at 14:45, Ebert,Timothy Aaron <tebert using ufl.edu> wrote:

>
> I had thought that mtcars in "mtcars$disp" was the name of a dataframe and
> that "disp" was the name of a column in the dataframe. If I would make a
> model like horse power = displacement then "disp" would be a variable in
> the model and I can find values for this variable in the "disp" column in
> the "mtcars" dataframe. I am not sure how I would use "mtcars" as a
> variable.
> "mtcars$disp" has no specific value, though it will have a specific value
> for any given row of data (assuming rows are observations).
>
> Tim
>
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Richard O'Keefe
> Sent: Tuesday, February 8, 2022 8:17 PM
> To: Erin Hodgess <erinm.hodgess using gmail.com>
> Cc: r-help using r-project.org
> Subject: Re: [R] Convert a character string to variable names
>
> [External Email]
>
> "mtcars$disp" is not a variable name.
> "mtcars" is a variable name, and
> get("mtcars") will get the value of that variable assign("mtcars",
> ~~whatever~~) will set it.
> mtcars$disp is an *expression*,
> where $ is an indexing operator
>
> https://urldefense.proofpoint.com/v2/url?u=https-3A__cran.r-2Dproject.org_doc_manuals_r-2Drelease_R-2Dlang.html-23Indexing&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=CI-7ZdIwlhUvhmOkVD7KJkv3IvSSWy4ix2Iz1netW81V-NUV8aOVVqyn5-fmD6cf&s=RjRC5kve6D8k59qZQYcX-PR-aA4TTu1yfLPBhHxSlWk&e=
> so what you want is
> > mtcars <- list(cyl=4, disp=1.8)
> > eval(parse(text="mtcars$disp"))
> [1] 1.8
>
> Though it's easy to do this, it's very seldom a good idea.
> The combination of parse and eval can do ANYTHING, no matter how
> disastrous.  Less powerful techniques are safer.
> Where do these strings come from in the first place?
> Why isn't it c("disp", "hp", "cyl")?
>
> On Tue, 8 Feb 2022 at 11:56, Erin Hodgess <erinm.hodgess using gmail.com> wrote:
>
> > Hello!
> >
> > I have a character string that is a vector of variable names.  I would
> > like to use those names to access the variables and create a matrix.
> > I tried the following:
> >
> > > .x
> >
> > [1] "mtcars$disp" "mtcars$hp"   "mtcars$cyl"
> >
> > > .y <- NULL
> >
> > > for(i in 1:3) {
> >
> > + .y[i] <- c(as.name(.x[[i]]))
> >
> > + }
> >
> > > .y
> >
> > [[1]]
> >
> > `mtcars$disp`
> >
> >
> > [[2]]
> >
> > `mtcars$hp`
> >
> >
> > [[3]]
> >
> > `mtcars$cyl`
> >
> >
> > But I am having trouble converting the variables in .y into a matrix.
> >
> >
> > I tried all kinds of stuff with bquote, deparse, do.call, but no good.
> >
> >
> > I have a feeling that it's something simple, and I'm just not seeing it.
> >
> >
> > Thanks,
> >
> > Erin
> >
> >
> >
> >
> > Erin Hodgess, PhD
> > mailto: erinm.hodgess using gmail.com
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mail
> > man_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAs
> > Rzsn7AkP-g&m=CI-7ZdIwlhUvhmOkVD7KJkv3IvSSWy4ix2Iz1netW81V-NUV8aOVVqyn5
> > -fmD6cf&s=c8oCLZK8TFAAs5d3vhDyB52KR2I9WWSTg6kDjL8orcI&e=
> > PLEASE do read the posting guide
> > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.or
> > g_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeA
> > sRzsn7AkP-g&m=CI-7ZdIwlhUvhmOkVD7KJkv3IvSSWy4ix2Iz1netW81V-NUV8aOVVqyn
> > 5-fmD6cf&s=fTO2Qrx6DmlzcB2uqN4fsDmTMVZwfCsDbLtzMigHWXI&e=
> > 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://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=CI-7ZdIwlhUvhmOkVD7KJkv3IvSSWy4ix2Iz1netW81V-NUV8aOVVqyn5-fmD6cf&s=c8oCLZK8TFAAs5d3vhDyB52KR2I9WWSTg6kDjL8orcI&e=
> PLEASE do read the posting guide
> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=CI-7ZdIwlhUvhmOkVD7KJkv3IvSSWy4ix2Iz1netW81V-NUV8aOVVqyn5-fmD6cf&s=fTO2Qrx6DmlzcB2uqN4fsDmTMVZwfCsDbLtzMigHWXI&e=
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list