Reputation: 617
I am working with a precipitation data set that is organised in an array (dimensions are 150 150 80
). The third dimension respresents the single time steps.
Additonally I have a matrix called "mask" of dimensions 150 150
. The goal is to multiply this matrix with every single time step so that at the end there are 80 (which is equal to ntime
) resulting matrices.
This can easily be done using a loop:
for (i in 1:ntime) {
assign(paste0("matrix_out",i), PRECIPITATION[,,i]*mask)
}
The thing is that afterwards I have to deal with 80 matrices, called "matrix_out1, matrix_out1, ..., matrix_out80". It is possible to further process these data, but not very elegant though.
I am sure that there is a more handy way by using a list instead of creating 80 single objects. So far I am not so familiar with working with lists.
Does anybody can give me a hint how to do that?
Upvotes: 0
Views: 597
Reputation: 2301
The simplest solution would be to add each loop result to a list:
matlist <- list()
for (i in 1:ntime) {
matlist[[i]] = assign(paste0("matrix_out",i), PRECIPITATION[,,i]*mask)
}
Right, I forgot about the matrices saved to the environment. You can eliminate that with:
matlist <- list()
for (i in 1:ntime) {
matlist[[i]] = PRECIPITATION[,,i]*mask
names(matlist)[i] = paste0("matrix_out",i)
}
So the stand-alone matrices are not created
Upvotes: 1