Julia236 - 4 days ago 6

R Question

In a first large Loop I generated 1,000,000 matrices XY_1_1, ..., XY_1000_1000:

`for (i in 1:1000){`

for (j in 1:1000){

assign(paste0("XY",i,j ,sep='_'), matrix(ncol=10, nrow=4))

}

}

Now I want to assign different outcomes of a function (vector of length 10) to the rth row of the matrices, which should somehow like this:

`for (r in 1:4){`

for (i in 1000){

for (j in 1:1000){

assign(paste0("XY",i,j ,sep='_')[r,], function(i,j,r))

}

}

}

Unfortunately, I get the error

`'incorrect number of dimensions'`

Furthermore, I tried using the

`get()`

`for (r in 1:4){`

for (i in 1000){

for (j in 1:1000){

get(paste0("XY",i,j ,sep='_'))[r,] <- function(i,j,r)

}

}

}

which brought the error

`'target of assignment expands to non-language object'`

Answer

How about

```
for (r in 1:4){
for (i in 1:1000){
for (j in 1:1000){
Row = function(i,j,r)
eval(parse(text = paste0("M_",i,"_",j, "[r,] <- Row")))
}
}
}
```

Although you might want to test this with the limits smaller than 1000 x 1000.

Source (Stackoverflow)

Comments