[R] Embedding lists in matrices and matrices in lists

Michael Kogan michael.kogan at gmx.net
Wed Aug 19 13:04:39 CEST 2009


Strange, it doesn't work for me:

Error in database[4, 4][[1]][1, ] : incorrect number of dimensions
Execution halted

R version 2.9.0 (2009-04-17) on Arch Linux, no additional packages 
installed.

David Winsemius schrieb:
>
> On Aug 19, 2009, at 6:02 AM, Michael Kogan wrote:
>
>> Unfortunately the  matrix(list(),x,y) way seems to have some 
>> limitations. I want to continue working with the matrices which are 
>> saved in the database matrix. But for example the following doesn't 
>> work:
>>
>> tetrahedron=matrix(c(
>> 0,1,1,1,
>> 1,0,1,1,
>> 1,1,0,1,
>> 1,1,1,0
>> ),nrow=4, byrow=TRUE) # an example matrix
>>
>> database=matrix(list(),5,5) # create database matrix
>> database[[4,4]]=list(tetrahedron) # save example matrix in database 
>> matrix
>>
>> ## try to access the first row of the saved example matrix ##
>> database[[4,4]][1] # prints the whole example matrix
>
> I was surprised that this worked. I would have expected that you would 
> have used:
> database[4,4][[1]]   since database is a matrix and generally one 
> accesses matrix elements
> with "[<n>,<m>]" addressing, while the element is a list and would 
> need "[[<n>]]" addressing.
>
>> database[[4,4]][1][1,] # should have printed the first row of the 
>> example matrix, but gives:
>
> So I tried:
>
> database[4,4][[1]][1,]
> # [1] 0 1 1 1 ....Success
>
>
>>
>> Error in database[[4, 4]][1][1,] : incorrect number of dimensions
>> Execution halted
>>
>> The same happens with database[[4,4]][1,]... Is there any way to 
>> access the saved matrices like "normal" ones?
>>
>> Thanks,
>> Michael
>>
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>




More information about the R-help mailing list