[R] implicit loop for nested list
Naresh Gurbuxani
n@re@h_gurbux@n| @end|ng |rom hotm@||@com
Fri Jan 27 04:33:25 CET 2023
>
> I am looking for a more elegant way to write below code.
>
> #Simulation results have different dimensions
> mysim <- lapply(1:10, function(y) {
> two.mat <- matrix(rnorm(4), nrow = 2)
> four.mat <- matrix(rnorm(16), nrow = 4)
> list(two.mat = two.mat, four.mat = four.mat) #results with different dimensions
> })
>
> #Collect different components of simulation results
> #Is it possible to do this with implicit loops?
> mat2 <- matrix(nrow = 2, ncol = 1)
> mat4 <- matrix(nrow = 4, ncol = 1)
> for (mat.list in mysim) {
> mat2 <- cbind(mat2, mat.list[["two.mat"]])
> mat4 <- cbind(mat4, mat.list[["four.mat"]])
> }
> mat2 <- mat2[,-1]
> mat4 <- mat4[,-1]
More information about the R-help
mailing list