[R] two dimensional array of object elements

Tony Plate tplate at acm.org
Fri Feb 11 21:51:02 CET 2005


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
 > 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?
>
>Thanks
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list