I am following the example I find on ?assign: a <- 1:4 assign("a[1]", 2) 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) } 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
Hi Matteo From assign help page x a variable name, given as a character string. No coercion is done, and the first element of a character vector of length greater than one will be used, with a warning Nothing is said about matrices and dimensions here so I wouldn't expect that assign can operate the way you are expecting. However I am not sure how to do what you want. It has probably something to do with environments which is not my cup of tea. I would do it list way. Here is an example> Named list with 2 matrices> lll<-list(mat_a=mat_a, mat_b=mat_b) > lll$mat_a [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 0 0 0 0 0 [4,] 0 0 0 0 0 [5,] 0 0 0 0 0 $mat_b [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 0 0 0 0 0 [4,] 0 0 0 0 0 [5,] 0 0 0 0 0 Name of a matrix> nam[1] "mat_a" Selection of appropriate matrix element and assigning it a value> lll[[which(names(lll)==nam)]][2,2]<-10Changed list> lll$mat_a [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 10 0 0 0 [3,] 0 0 0 0 0 [4,] 0 0 0 0 0 [5,] 0 0 0 0 0 $mat_b [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 0 0 0 0 0 [4,] 0 0 0 0 0 [5,] 0 0 0 0 0 You can easily put everything in a function similar to yours without using assign. Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Matteo > Richiardi > Sent: Wednesday, December 23, 2015 9:45 AM > To: r-help at r-project.org > Subject: [R] assigning values to elements of matrixes > > I am following the example I find on ?assign: > > a <- 1:4 > assign("a[1]", 2) > > 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) > } > > 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
Hi, How about the following: foo2 <- function(s,i,j,value) { M = get(paste("M_",s,sep="")) M[i,j] = value assign(paste("M_",s,sep=""),M, envir = .GlobalEnv) } foo2("a",1,2,15) cheers Peter> On 23 Dec 2015, at 09:44, Matteo Richiardi <matteo.richiardi at gmail.com> wrote: > > I am following the example I find on ?assign: > > a <- 1:4 > assign("a[1]", 2) > > 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) > } > > 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.
Dear Matteo, Here's a function that does what you want and that you should be able to adapt to your problem. It does have the disadvantage of copying the object twice (beyond the overhead of assigning to the indexed value): myassign <- function(x, index, value, envir=.GlobalEnv, ...){ object <- get(x, envir=envir, ...) object[index] <- value assign(x, object, envir=envir, ...) } Your example:> a <- 1:4 > myassign("a", 1, 2) > a[1] 2 2 3 4 I hope this helps, John ----------------------------- John Fox, Professor McMaster University Hamilton, Ontario Canada L8S 4M4 Web: socserv.mcmaster.ca/jfox> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Matteo > Richiardi > Sent: December 23, 2015 3:45 AM > To: r-help at r-project.org > Subject: [R] assigning values to elements of matrixes > > I am following the example I find on ?assign: > > a <- 1:4 > assign("a[1]", 2) > > 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) > } > > 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.
> 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. -- David.> > 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
Dear all, Thanks very much for your help. This completely clarifies my question. Mayteo Il giorno 23/dic/2015 17:05, "Anthoni, Peter (IMK)" <peter.anthoni at kit.edu> ha scritto:> Hi, > > How about the following: > foo2 <- function(s,i,j,value) > { > M = get(paste("M_",s,sep="")) > M[i,j] = value > assign(paste("M_",s,sep=""),M, envir = .GlobalEnv) > } > > foo2("a",1,2,15) > > cheers > Peter > > > On 23 Dec 2015, at 09:44, Matteo Richiardi <matteo.richiardi at gmail.com> > wrote: > > > > I am following the example I find on ?assign: > > > > a <- 1:4 > > assign("a[1]", 2) > > > > 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) > > } > > > > 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. > >[[alternative HTML version deleted]]