[R] Embedding lists in matrices and matrices in lists

Petr PIKAL petr.pikal at precheza.cz
Wed Aug 19 14:42:04 CEST 2009


Hi

r-help-bounces at r-project.org napsal dne 19.08.2009 13:04:39:

> 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.

database[4,4][[1]][,1]

does not work for me either but it is result of your complicated structure 
of nested lists

This works

> database[4,4][[1]][[1]][,1]
[1] 0 1 1 1

See the structure

Matrix of lists

> database
     [,1] [,2] [,3] [,4]   [,5]
[1,] NULL NULL NULL NULL   NULL
[2,] NULL NULL NULL NULL   NULL
[3,] NULL NULL NULL NULL   NULL
[4,] NULL NULL NULL List,1 NULL
[5,] NULL NULL NULL NULL   NULL

List of lists
> database[4,4]
[[1]]
[[1]][[1]]
     [,1] [,2] [,3] [,4]
[1,]    0    1    1    1
[2,]    1    0    1    1
[3,]    1    1    0    1
[4,]    1    1    1    0

List which contains your matrix

> database[4,4][[1]]
[[1]]
     [,1] [,2] [,3] [,4]
[1,]    0    1    1    1
[2,]    1    0    1    1
[3,]    1    1    0    1
[4,]    1    1    1    0

Here is your matrix

> database[4,4][[1]][[1]]
     [,1] [,2] [,3] [,4]
[1,]    0    1    1    1
[2,]    1    0    1    1
[3,]    1    1    0    1
[4,]    1    1    1    0
>

Regards
Petr


> 
> 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
> >
> 
> ______________________________________________
> 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