[R] Convert a character string to variable names

Jeff Newmiller
Sun, 13 Feb 2022 06:54:48 -0000

Value is all the information in the object. In R, for pretty much everything
except environment objects the two terms are interchangeable (object is
equivalent to value) because R works hard to make it so.

For environment objects (the exception), the contents of the environment can
change even while different variables continue to refer to the same environment
object. This is how f(3) ends up as c(7,6) in the following code:

x <- c(3,4)
f <- function(a) x+a
x <- c(4,3)
f(3)

The f object knows to look in the global environment for x from when it was
created, but the x name embedded in that environment was replaced with a
different object by the time f was called. That is, x referred to an object
containing a value c(3,4) first, and then later to a completely different
location in memory (object identity) with a different value c(4,3). The same
global environment object thus contained different information at different
times, even though the identity (address) of the global environment object was
never changed, so f could store that identity information for use later.

If you use read.csv to import a data set into a data frame and assign that to a
variable name DF, all of that information represents the value of that data
frame. If you change an element of the first column:

DF[1,1] <- 9

then R will only modify the value of that data frame object in place if no other
variable is referring to it... that is, if you have passed DF to a function then
it makes a whole new object when you try to change part of it in the function.
But the details about changing parts of an object this way are mostly useful for
avoiding inefficient memory use... while the fact that the value of an object
consists of all of the information in it is useful from a statistical analysis
point of view... different values in the large sense in general lead to
different results. A specific example being that an lm object has a value based
on the input objects that were used to create it.

See http://adv-r.had.co.nz/memory.html for a more complete discussion.

On February 12, 2022 7:24:18 PM PST, "Ebert,Timothy Aaron" <tebert
at ufl.edu> wrote:
>How does ?a value? differ from ?an object??
>
>From: Richard O'Keefe <raoknz at gmail.com>
>Sent: Friday, February 11, 2022 12:25 AM
>To: Ebert,Timothy Aaron <tebert at ufl.edu>
>Cc: Jeff Newmiller <jdnewmil at dcn.davis.ca.us>; r-help at
r-project.org; Erin Hodgess <erinm.hodgess at gmail.com>
>Subject: Re: [R] Convert a character string to variable names
>
>[External Email]
>You wrote "32 numbers is not a value".
>It is, it really is.  When you have a vector like
> x <- 1:32
>you have a simple variable (x) referring to an immutable value
>(1, 2, ..., 32).  A vector in R is NOT a collection of mutable
>boxes, it is a collection of *numbers* (or strings).  The vector
>itself is a good a value as ever twanged.  You cannot change it.
>A statement like
> x[i] <- 77
>is just shorthand for
> x <- "[<-"(x, i, 77)
>which constructs a whole new 32-number value and assigns that to x.
>(The actual implementation is cleverer when it can be, but often it
>cannot be clever.)
>Pure values like vectors can be shared: if x is a vector,
>then y <- x is a constant time operation.  If you then change
>y, you only change y, not the vector.  x is unchanged.
>
>
>On Wed, 9 Feb 2022 at 17:06, Ebert,Timothy Aaron <tebert at
ufl.edu<mailto:tebert at ufl.edu>> wrote:
>"A variable in R can refer to many things, ..." I agree.
>"It absolutely _can_ refer to a list, ..." I partly agree. In R as
a programming language I agree. In R as a statistical analysis tool then only
partly. Typically one would need to limit the list so each variable would be of
the same length and all values within the variable be of the same data type
(integer, real, factor, character). As a programmer yes, as a statistician not
really unless you always qualify the type of list considered and that gets
tiresome.
>
>R does name individual elements using numeric place names: hence df[row,
column]. Each element must have a unique address, and that is true in all
computer languages.
>
>A dataframe is a list of columns of the same length containing the same data
type within a column.
>
>mtcars$disp does not have a value (a value is one number). With 32 elements
I can calculate a mean and the mean is a value. 32 numbers is not a value. I
suppose a single value could be the starting memory address of the name, but I
don't see how that distinction helps unless one is doing Assembly or Machine
language programming.
>
>I have never used get(), so I will keep that in mind. I agree that it makes
life much easier to enter the data in the way it will be analyzed.
>
>
>
>
>-----Original Message-----
>From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us<mailto:jdnewmil at
dcn.davis.ca.us>>
>Sent: Tuesday, February 8, 2022 10:10 PM
>To: r-help at r-project.org<mailto:r-help at r-project.org>;
Ebert,Timothy Aaron <tebert at ufl.edu<mailto:tebert at ufl.edu>>;
Richard O'Keefe <raoknz at gmail.com<mailto:raoknz at
gmail.com>>; Erin Hodgess <erinm.hodgess at
gmail.com<mailto:erinm.hodgess at gmail.com>>
>Cc: r-help at r-project.org<mailto:r-help at r-project.org>
>Subject: Re: [R] Convert a character string to variable names
>
>[External Email]
>
>A variable in R can refer to many things, but it cannot be an element of a
vector. It absolutely _can_ refer to a list, a list of lists, a function, an
environment, and any of the various kinds of atomic vectors that you seem to
think of as variables. (R does _not_ name individual elements of vectors, unlike
many other languages.)
>
>The things you can do with the mtcars object may be different than the
things you can do with the object identified by the expression mtcars$disp, but
the former has a variable name in an environment while the latter is embedded
within the former. mtcars$disp is shorthand for the expression mtcars[[
"disp" ]] which searches the names attribute of the mtcars list (a
data frame is a list of columns) to refer to that object.
>
>R allows non-standard evaluation to make elements of lists accessible as
though they were variables in an environment, such as with( mtcars, disp ) or
various tidyverse evaluation conventions. But while the expression mtcars$disp
DOES have a value( it is an atomic vector of 32 integer elements) it is not a
variable so get("mtcars$disp") cannot be expected to work (as it does
not). You may be confusing "variable" with "object" ... lots
of objects have no variable names.
>
>I have done all sorts of complicated data manipulations in R, but I have
never found a situation where a use of get() could not be replaced with a
clearer way to get the job done. Using lists is central to this... avoid making
distinct variables in the first place if you plan to be retrieving them later
indirectly like this.
>
>On February 8, 2022 5:45:39 PM PST, "Ebert,Timothy Aaron"
<tebert at ufl.edu<mailto:tebert at 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 at
r-project.org<mailto:r-help-bounces at r-project.org>> On Behalf Of
Richard
>>O'Keefe
>>Sent: Tuesday, February 8, 2022 8:17 PM
>>To: Erin Hodgess <erinm.hodgess at gmail.com<mailto:erinm.hodgess
at gmail.com>>
>>Cc: r-help at r-project.org<mailto:r-help at 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.o
>>rg_doc_manuals_r-2Drelease_R-2Dlang.html-23Indexing&d=DwICAg&c=sJ6xIWYx
>>-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=CI-7ZdIwlhUvhmOkVD7KJkv3IvSSW
>>y4ix2Iz1netW81V-NUV8aOVVqyn5-fmD6cf&s=RjRC5kve6D8k59qZQYcX-PR-aA4TTu1yf
>>LPBhHxSlWk&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 at
gmail.com<mailto:erinm.hodgess at 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<https://urldefense.proofpoint.com/v2/url?u=http-3A__as.name&d=DwMFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=vzlTpQd9zYJkQ77y8VRROjzMQQJrJce_5rInko9TViGjuIt93PxagLXs9prJsMwy&s=Yrczdj8QHFrWSBSm_k4WyKN7ppY20M360b7tUmMCJaY&e=>(.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 at gmail.com<mailto:erinm.hodgess at
gmail.com>
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org<mailto:R-help at r-project.org>
mailing list -- To UNSUBSCRIBE and more, see
>>>
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mai
>>> l
>>>
man_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeA
>>> s
>>>
Rzsn7AkP-g&m=CI-7ZdIwlhUvhmOkVD7KJkv3IvSSWy4ix2Iz1netW81V-NUV8aOVVqyn
>>> 5
-fmD6cf&s=c8oCLZK8TFAAs5d3vhDyB52KR2I9WWSTg6kDjL8orcI&e>>>
PLEASE do read the posting guide
>>>
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.o
>>> r
>>>
g_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVe
>>> A
>>>
sRzsn7AkP-g&m=CI-7ZdIwlhUvhmOkVD7KJkv3IvSSWy4ix2Iz1netW81V-NUV8aOVVqy
>>> n
5-fmD6cf&s=fTO2Qrx6DmlzcB2uqN4fsDmTMVZwfCsDbLtzMigHWXI&e>>> and
provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>        [[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help at r-project.org<mailto:R-help at r-project.org> mailing
list -- To UNSUBSCRIBE and more, see
>>https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailm
>>an_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRz
>>sn7AkP-g&m=CI-7ZdIwlhUvhmOkVD7KJkv3IvSSWy4ix2Iz1netW81V-NUV8aOVVqyn5-fm
>>D6cf&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=9PEhQh2kVeAsR
>>zsn7AkP-g&m=CI-7ZdIwlhUvhmOkVD7KJkv3IvSSWy4ix2Iz1netW81V-NUV8aOVVqyn5-f
>>mD6cf&s=fTO2Qrx6DmlzcB2uqN4fsDmTMVZwfCsDbLtzMigHWXI&e>>and
provide commented, minimal, self-contained, reproducible code.
>>
>>______________________________________________
>>R-help at r-project.org<mailto:R-help at r-project.org> mailing
list -- To UNSUBSCRIBE and more, see
>>https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailm
>>an_listinfo_r-2Dhelp&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRz
>>sn7AkP-g&m=jyG_tiJYdPBF8hat6uuafk5_ucrnBk_CkkVVmV3SLbXFMTeEFy-zgo7hVDFc
>>iokP&s=6B9_2qIT3ZzL4bGqJfWfMBQofnf6I2_bpLvdQIMDXj0&e>>PLEASE
do read the posting guide
>>https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org
>>_posting-2Dguide.html&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsR
>>zsn7AkP-g&m=jyG_tiJYdPBF8hat6uuafk5_ucrnBk_CkkVVmV3SLbXFMTeEFy-zgo7hVDF
>>ciokP&s=TTQhZrau_AmlW41w76jtlT7yR-niL17-f1QgYsWePvQ&e>>and
provide commented, minimal, self-contained, reproducible code.
>
>--
>Sent from my phone. Please excuse my brevity.

--
Sent from my phone. Please excuse my brevity.