[R] assigning values to elements of matrixes

David Winsemius dwinsemius at comcast.net
Wed Dec 23 20:16:19 CET 2015

> On Dec 23, 2015, at 12:44 AM, Matteo Richiardi <matteo.richiardi at gmail.com> wrote:
> I am following the example I find on ?assign:
> a <- 1:4
> assign("a[1]", 2)

You appear to have completely misinterpreted the intent of the authors of that help page. The next two lines in that example (which you omitted) show that they intended you to see this as a demonstration of what NOT to do:

a <- 1:4
assign("a[1]", 2)
a[1] == 2          # FALSE
get("a[1]") == 2   # TRUE

Notice that a[1] was not assigned the value of 2 as demonstrated when the logical test returning FALSE, but that that an object with the pathological name `a[1]` was created.

> This appears to create a new variable named "a[1]" rather than
> changing the value of the vector.
> Am I missing something here? How can I assign a value to a specified
> element of a vector/matrix?
> Of course, my problem is slightly more involved, but I guess the above
> is its core. For those interested, I have the two matrixes
> M_a <- matrix(0,10,10)
> M_b <- matrix(0,10,10)
> I want to have a function that assigns a value to a specific element
> of one of the matrix, as in
> foo <- function(s,i,j,value){
>  assign(paste("M_",s)[i,j],value)
> }

I see that Peter Antoni and John Fox have offered a demonstration of how to use get and assign. 

Those efforts at macro-ization are essentially recapitulating what happens when using:

M_a[5,5] <- value

The M_a object is looked up, copied to something named `*tmp*`, and then the assignment within that copy is performed and the value copied back into M_a. These semantics with there attendant inefficiencies have prompted the development of the data.table package where the assignments are done in place.

The alternate strategy of Giorgio Garzanio calls to mind this fortune:

> fortunes::fortune("parse()")

If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
      R-help (February 2005)

See pages 11-13 of https://cran.r-project.org/doc/Rnews/Rnews_2001-3.pdf to see how Lumley expands on this theme.


> This however does not work:
> foo('a',1,1,1)
> Error in paste("M_", s)[1, j] : incorrect number of dimensions
> Following the ?assign help, I tried
> foo2 <- function(s,i,j,value){
>  assign(paste("M_",s,"[i,j]"),value, envir = .GlobalEnv)
> }
> but this produces the problem I described above (namely, a new
> variable is created, rather than replacing the specified element of
> the matrix).
> I know that in this example I could simply pass the matrix as an
> argument to the function, but I am interested in understanding how
> 'assign' work.
> Many thanks for your help.
> Matteo
> ______________________________________________
> R-help at 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.

David Winsemius
Alameda, CA, USA

More information about the R-help mailing list