Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize a list of matrices in R

Tags:

list

r

matrix

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!

like image 264
Michael Avatar asked Jan 11 '13 18:01

Michael


People also ask

How do you create a matrix list in R?

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.

Can you have a list of matrices in R?

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.

What is the difference between list and matrix in R?

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.


1 Answers

Also try this wrapper of *apply:

replicate(10, diag(2), simplify=F)
like image 102
Apprentice Queue Avatar answered Sep 30 '22 13:09

Apprentice Queue