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
Weijie Cai <wcai11 <at> hotmail.com> writes:
:
: 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
: }
: }
:
A list that has a dim attribute should do it:
R> x <- list(12, "abc", 3i, sin)
R> dim(x) <- c(2,2)
R> x
[,1] [,2]
[1,] 12 0+3i
[2,] "abc" ?
R> x[[2,2]](pi/180)
[1] 0.01745241
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
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?