# [R] problems with assign

Duncan Murdoch murdoch at stats.uwo.ca
Tue Apr 11 20:48:58 CEST 2006

```On 4/11/2006 2:30 PM, Arnau Mir wrote:
> Hello.
>
> I have n files in a directory: file1, ..., filen.
>
> I read them with the following commands:
>
> list=scan(file="list",what=list(nom=""))
> # in the file list, I have all the filenames.
> n=length(list[[1]])
>
> for (i in 1:n){
>     aux <- paste("p",i,sep="")
> }
>
> R creates the matrices p1,p2,...,pn.
>
> I want to manipulate the matrices p1,...,pn and create new matrices
> pp1,...,ppn. The dimensions of matrices ppi must be 100x100.
> So, I do the following:
>
> for (i in 1:n){
>      aux2 <- paste("pp",i,sep="")
>      assign(aux2, array(0,dim=c(100,100)))
> }
>
> My question is the following:
>
> I want to assign to the matrix ppi[1,1] the value of pi[1,1] for i=1,...,n.
> How can I do it?
>
>
> For i=1, I try:
>
>
> but it doesn't work because if I ask R:
> pp1[1,1]
> R responses 0 but the value of p1[1,1] is not 0.

Despite its appearance, the statement

pp2[1,1] <- p2[1,1]

doesn't do an assignment to pp2[1,1], it does an assignment to pp2 of
the result of a call to the function "[<-".  See the R Language
Definition, "Function Calls" section.

I really don't recommend working the way you want to.  You'd be much
better off leaving everything in lists, say pp to hold your pp1 to ppn,
and p to hold p1 to pn.  Then you could say

pp[[2]][1,1] <- p[[2]][1,1]

Duncan Murdoch

```