Skip to content

How to assign value to a variable dynamically constructed

7 messages · Jun Shen, Greg Snow, William Dunlap +2 more

#
Dear list,

Let's say we have a variable (id), whose name is dynamically constructed.
This variable represents a vector or data frame with many elements. Now I
want to specifically assign a value to one of the elements. I couldn't get
it right.

test <- 'id' # "id" is dynamically constructed through paste()

id <- 1:4

# I can get the element by doing

get(test)[2]

# Now I want to assign a value to the second element of this dynamical
variable.

get(test)[2] <- 5  # doesn't work.

Thanks a lot.

Jun Shen
#
Try reading the Help files before posting here. That's what they're for.

?get

provides the answer in a note in the Help page.

Cheers,
Bert


Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll
On Tue, Jul 7, 2015 at 1:53 PM, Jun Shen <jun.shen.ut at gmail.com> wrote:
#
This is FAQ 7.21.

The most important part of the answer in FAQ 7.21 is the last section
where it states that it is often easier to use a list rather than
messing around with trying to dynamically name global variables.

If you tell us what you are trying to accomplish then we may have
better advice.  The route you are headed down now usually leads to
inefficient code and hard to find bugs.
On Tue, Jul 7, 2015 at 2:53 PM, Jun Shen <jun.shen.ut at gmail.com> wrote:

  
    
#
You can use an environment instead of a list using the same [[ syntax.  It
is like 'get0(..., inherit=FALSE)' on the left side of the <- and like
'assign(...)' on the right side.   E.g.,
   myData <- new.env()
   varName <- "v1"
   myData[[varName]] <- 1:10
   myData[[varName]][4] <- myData[[varName]][4] * 100
   myData[[varName]]
   #  [1]   1   2   3 400   5   6   7   8   9  10
   names(myData)
   # [1] "v1"
(Before R-3.2.0 or so, you had to use objects(myData,all=TRUE) if
myData was an environment and names(myData) if it was a list.  Now
names() works for environments.)

It is better to use a dedicated environment (or list) for each set of
related
variables so that name collisions do not cause problems.


Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Wed, Jul 8, 2015 at 10:06 AM, Greg Snow <538280 at gmail.com> wrote:

            

  
  
1 day later
#
Dear all,

Provided I understood correctly, shouldn't assign() do the trick? Most similar threads seem to include this approach (among others, indeed).

Regards,
Bastien
On Wednesday, July 8, 2015 at 7:30:04 PM UTC+2, William Dunlap wrote:
#
Yes, assign() and get() can do this, but I think the [[]] syntax is simpler
and makes it easier to switch between lists and environments for
data organization.

E.g., the translation of
   myData[[varName]][4] <- myData[[varName]][4] * 100
where myData is an environment to the get/assign style
would be something like
   tmp <- get(varName, envir=myData)
   tmp[4] <- tmp[4] * 100
   assign(varName, tmp, envir=myData)

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Jul 10, 2015 at 9:44 AM, Bastien Tran <bastien.tran at gmail.com>
wrote:

  
  
#
I'll let Bill respond in detail if he cares to(he is both more
knowledgable and fluent at this than I), but as a nearly unbreakable
rule, get() and assign() should not be used in R. Basically, they
represent a macro (script)-oriented strategy for handling R's objects,
whereas R is designed to use an object-oriented ("everything is an
object") and functional ("all procedures are functions") approach.
Using get() and assign() leads to messy, confusing, error-prone,
non-portable code in R, and that's why they should be avoided.

For details, you should consult web tutorials, John Chambers's books,
and other books on R programming.

Cheers,
Bert


Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll
On Fri, Jul 10, 2015 at 9:44 AM, Bastien Tran <bastien.tran at gmail.com> wrote: