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]
implicit loop for nested list
5 messages · Naresh Gurbuxani, Bert Gunter, Jeff Newmiller +1 more
Is this what you want: ## This cbinds all the 2 matrix components of mysim ## producing a 2 x 20 matrix do.call(cbind,lapply(mysim,`[[`,1)) ## Change the 1 to a 2 to cbind the other components. Cheers, Bert Tha On Thu, Jan 26, 2023 at 7:33 PM Naresh Gurbuxani <
naresh_gurbuxani at hotmail.com> wrote:
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]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Elegance is in the eyes of the beholder...
extractor <- function( simlist, sim_name ) {
do.call(
cbind
, lapply(
simlist
, function( r ) r[[ sim_name ]]
)
)
}
extractor( mysim, "two.mat" )
... but using do.call will be much more memory efficient than successive cbind operations.
On January 26, 2023 7:33:25 PM PST, Naresh Gurbuxani <naresh_gurbuxani at hotmail.com> wrote:
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]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Sent from my phone. Please excuse my brevity.
I would use replicate() to do an operation with random numbers repeatedly:
```
mysim <- replicate(10, {
two.mat <- matrix(rnorm(4), 2, 2)
four.mat <- matrix(rnorm(16), 4, 4)
list(two.mat = two.mat, four.mat = four.mat)
})
```
which should give you a matrix-list. You can slice this matrix-list
just like normal, then cbind it in one step:
```
two.mat <- do.call("cbind", mysim["two.mat", ])
four.mat <- do.call("cbind", mysim["four.mat", ])
```
On Thu, Jan 26, 2023 at 10:33 PM Naresh Gurbuxani
<naresh_gurbuxani at hotmail.com> wrote:
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]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Thanks everyone for their solutions. My problem is solved. Sent from my iPhone
On Jan 27, 2023, at 12:17 AM, Andrew Simmons <akwsimmo at gmail.com> wrote:
?I would use replicate() to do an operation with random numbers repeatedly:
```
mysim <- replicate(10, {
two.mat <- matrix(rnorm(4), 2, 2)
four.mat <- matrix(rnorm(16), 4, 4)
list(two.mat = two.mat, four.mat = four.mat)
})
```
which should give you a matrix-list. You can slice this matrix-list
just like normal, then cbind it in one step:
```
two.mat <- do.call("cbind", mysim["two.mat", ])
four.mat <- do.call("cbind", mysim["four.mat", ])
```
On Thu, Jan 26, 2023 at 10:33 PM Naresh Gurbuxani <naresh_gurbuxani at hotmail.com> wrote:
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]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.