I was wondering if there's a quick way to initialize a list of matrices in R. For example I'm looking for a (one-liner) to reproduce the same results as the following:
output_array = list()
for(i in 1:10){
output_array[i] = diag(2)
}
Thanks!
list() converts the matrix to a list of lists in column-major order. Therefore, we have to use unlist() function to convert the list of lists to a single list. unlist() function in R Language is used to convert a list of lists to a single list, by preserving all the components.
Create Matrix using List. Matrices are created using matrix() function in R programming. Another function that will be used is unlist() function to convert the lists into a vector.
A list will create one column for each element; it's an error if they're not all the same length. A matrix will create a data frame with the same number of columns and rows as the matrix.
Also try this wrapper of *apply:
replicate(10, diag(2), simplify=F)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With