R equivalent matrix "matrix matrix"

Does anyone know how to make the R equivalent of cell (2,2) in Matlab?

In Matlab, this creates a 2x2 "matrix" where each cell can be any datatype like another matrix or whatever.

Basically, it could be a matrix of matrices if that's what the user wants.

Is there a way to do this in R?

+3


source to share


1 answer


You can create an object like this with

mm<-matrix(list(), 2, 2)

      

But note that the indexing operators are slightly different. To fetch / assign one cell you have to use



mm[[1,1]]<-matrix(1:15, nrow=3)
mm[[1,2]]<-"hello"
mm[[2,1]]<-list(a=1, b=2)
mm[[2,2]]<-2

      

pay attention to [[ , ]]

, not the typical [, ]

"standard" matrix. Using only one [ , ]

will return the list of items you are requesting as a standard list.

And as @joran pointed out, most functions in R don't expect an object of this type, so don't expect functions that work with matrices to automatically work with a matrix of lists like this

+7


source







All Articles