[R] Matrixes as data

Gabor Grothendieck ggrothendieck at gmail.com
Sat Oct 17 00:36:29 CEST 2009


Create a matrix out of a list.  In this example column 1 contains
matrices, column 2 contains the number 1 and 2 and column 3 contains
letters:

> L <- matrix(list(m, m+10, 1, 2, "a", "b"), 2); L
     [,1]      [,2] [,3]
[1,] Integer,4 1    "a"
[2,] Numeric,4 2    "b"
> L[[2,1]]
     [,1] [,2]
[1,]   11   13
[2,]   12   14



On Fri, Oct 16, 2009 at 12:18 PM, Kjetil Halvorsen
<kjetilbrinchmannhalvorsen at gmail.com> wrote:
> Thanks. The points of having the column of matrices (all the same dimension)
> in a data.frame, is that there are also other data, each matrix is at
> a location, so there are geographical
> coordinates and possibly other measurements at the same location.
>
> Kjetil
>
> On Fri, Oct 16, 2009 at 12:46 PM, Barry Rowlingson
> <b.rowlingson at lancaster.ac.uk> wrote:
>> On Fri, Oct 16, 2009 at 4:36 PM, Kjetil Halvorsen
>> <kjetilbrinchmannhalvorsen at gmail.com> wrote:
>>> Hola!
>>>
>>> I am working on a problem where data points are (square) matrices. Is
>>> there a way to make a
>>> "vector" of matrices, such that it can be stored in a data.frame? Can
>>> that be done with S3?
>>> or do I have to learn S4 objects & methods?
>>>
>>
>>  If the matrices are all the same size then you could store them in an
>> array, which is essentially a 3 or more dimensional matrix.
>>
>>  Otherwise, you can store them in a list, and get them by number:
>>
>> foo = list(matrix(1:9,3,3),matrix(1:16,4,4))
>> foo[[1]]
>> foo[[2]]
>>
>> and so forth.
>>
>> You'll only need to create new object classes (with S3 or S4) if you
>> want special behaviour of vectors of these things (such as plot(foo)
>> doing something sensible).
>>
>>  With S3 it's easy:
>>
>> class(foo)="squareMatrixVector"
>>
>> plot.squareMatrixVector=function(x,y,...){
>>  cat("ouch\n")
>> }
>>
>>  plot(foo)
>> ouch
>>
>> Barry
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>




More information about the R-help mailing list