[R] assigning values to elements of matrixes

PIKAL Petr petr.pikal at precheza.cz
Wed Dec 23 12:50:13 CET 2015

```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]<-10

Changed 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
> 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.
```