[R] Matrix

David Winsemius dwinsemius at comcast.net
Sun Jul 17 20:47:18 CEST 2016


> On Jul 16, 2016, at 7:43 PM, Ashta <sewashm at gmail.com> wrote:
> 
> HI Denes, Duncan,Michael and all,
> 
> Thank you very much  for the helpful suggestion.  Some of my data sets
> were not square matrix, however, Denes's suggestion,"
> as.data.frame.table() ", handled that one.
> 

`as.data.frame.table` should work with any matrix, not just sqaure ones:

m <- matrix(1:12, 3, 4,
           dimnames = list(dimA = letters[1:3],
                           dimB = letters[1:4]))
m
#-----
    dimB
dimA a b c  d
   a 1 4 7 10
   b 2 5 8 11
   c 3 6 9 12
#--------
 as.data.frame.table(m, responseName = "value")
   dimA dimB value

1     a    a     1
2     b    a     2
3     c    a     3
4     a    b     4
5     b    b     5
6     c    b     6
7     a    c     7
8     b    c     8
9     c    c     9
10    a    d    10
11    b    d    11
12    c    d    12




> Thank you again.
> 
> 
> On Sat, Jul 16, 2016 at 7:27 PM, Dénes Tóth <toth.denes at ttk.mta.hu> wrote:
>> 
>> 
>> On 07/17/2016 01:39 AM, Duncan Murdoch wrote:
>>> 
>>> On 16/07/2016 6:25 PM, Ashta wrote:
>>>> Hi all,
>>>> 
>>>> I have a large square matrix (60 x 60)  and found it hard to
>>>> visualize. Is it possible to change it  as shown below?
>>>> 
>>>> Sample example (3 x 3)
>>>> 
>>>>    A   B   C
>>>> A  3   4   5
>>>> B  4   7   8
>>>> C  5   8   9
>>>> 
>>>> Desired output
>>>> A A  3
>>>> A B  4
>>>> A C  5
>>>> B B  7
>>>> B C  8
>>>> C C  9
>>> 
>>> Yes, use matrix indexing.  I don't think the 3600 values are going to be
>>> very easy to read, but here's how to produce them:
>>> 
>>> m <- matrix(1:3600, 60, 60)
>>> indices <- expand.grid(row = 1:60, col = 1:60)
>>> cbind(indices$row, indices$col, m[as.matrix(indices)])
>>> 
>> 
>> Or use as.data.frame.table():
>> 
>> m <- matrix(1:9, 3, 3,
>>            dimnames = list(dimA = letters[1:3],
>>                            dimB = letters[1:3]))
>> m
>> as.data.frame.table(m, responseName = "value")
>> 
>> ---
>> 
>> I do not know what you mean by "visualize", but image() or heatmap() are
>> good starting points if you need a plot of the values. If you really need to
>> inspect the raw values, you can try interactive (scrollable) tables, e.g.:
>> 
>> library(DT)
>> m <- provideDimnames(matrix(1:3600, 60, 60))
>> datatable(m, options = list(pageLength = 60))
>> 
>> 
>> Cheers,
>>  Denes
>> 
>> 
>> 
>> 
>>> Duncan Murdoch
>>> 
>>> ______________________________________________
>>> 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.
> 
> ______________________________________________
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list