[R] two dimensional array of object elements

Gabor Grothendieck ggrothendieck at myway.com
Fri Feb 11 22:08:09 CET 2005


Tony Plate <tplate <at> acm.org> writes:

: 
: Create your original matrix as a list datatype.  When assigning elements, 
: be careful with the list structure, as the example indicates.
: 
:  > m <- 2; n <- 3
:  > a <- array(list(),c(m,n))
:  > a[1,2] <- list(b=1,c=2)
: Error in "[<-"(`*tmp*`, 1, 2, value = list(b = 1, c = 2)) :
:          number of items to replace is not a multiple of replacement length

Try:

a[[1,2]] <- list(b=1,c=2)


:  > a[1,2] <- list(list(b=1,c=2))
:  >
: 
: At Friday 11:36 AM 2/11/2005, Weijie Cai wrote:
: >Hi list,
: >
: >I want to create a two (possibly three) dimensional array of objects. 
: >These objects are classes in object oriented style. I failed by using
: >a<-array(NA,c(m,n))
: >for (i in 1:m){
: >  for (j in 1:n){
: >    a[i,j]<-My.Obj
: >  }
: >}
: >
: >The elements are still NA. Any suggestions?




More information about the R-help mailing list