s-news
[Top] [All Lists]

Forming iteratively 3-way/4-way tables

To: <s-news@lists.biostat.wustl.edu>
Subject: Forming iteratively 3-way/4-way tables
From: "Ray Haraf" <rayharaf@rogers.com>
Date: Sun, 24 Feb 2002 12:34:28 -0500
Hello,
 
To produce iteratively (row-1) by col matrices combining adjacent rows, I run the following:
> mat<-rbind(c(0,0,1,2),c(1,1,3,0),c(2,0,0,3))
> rows <- dim(mat)[1]
> cols <- dim(mat)[2]
> for (index in 1:(rows-1)){
+ print(paste("row combine: index =",index,"rows = ",rows))
+ newmat <- matrix(NA,nrow=rows-1,ncol=cols)
+ for (i in 1:(index-1))
+ for (j in 1:cols)
+ newmat[i,j] <- mat[i,j]
+ for (j in 1:cols)
+ newmat[index,j] <- mat[index,j] + mat[index+1,j]
+ if (index+2 <= rows)
+ for (i in (index+2):rows)
+ for (j in 1:cols)
+ newmat[i-1,j] <- mat[i,j]             
+ print(newmat)
+ }
[1] "row combine: index = 1 rows =  3"
     [,1] [,2] [,3] [,4]
[1,]    1    1    4    2
[2,]    2    0    0    3
[1] "row combine: index = 2 rows =  3"
     [,1] [,2] [,3] [,4]
[1,]    0    0    1    2
[2,]    3    1    3    3
>
I unsuccessfully tried to extend to three-way then, ultimately, to four-way tables. I cannot modify arguments in "table()" as I can do in "matrix()". I will be very appreciative of any suggestion or help.
 
Thanks in advance,
Ray Haraf.
<Prev in Thread] Current Thread [Next in Thread>
  • Forming iteratively 3-way/4-way tables, Ray Haraf <=