[R] Entering data into a multi-way array?

Victoria_Stuart 1 at VictoriasJourney.com
Sat Oct 8 23:13:59 CEST 2011


Update:

1. Here is a better illustration.  I assumed that the gene expression was
the same for the three replicates for each gene, in the respective Time and
Treatment groups, so that I could follow (verify) what was going on.

2. To better-view the following, copy and paste into a plain-text editor
with a monospace font (e.g. gedit on Ubuntu with the Monospace font).

Sincerely, Victoria  :-)

 ------------------------------------

5 Genes x 3 Replicates x 2 Times x 2 Treatments

> x<-matrix(scan("/home/victoria/R/5.3.2.2.csv",n=5*12),5,12,byrow=TRUE)
Read 60 items

> x
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]    1    1    1    2    2    2    3    3    3     4     4     4   #
Gene1 avg. = row avg. = 2.5
[2,]    1    1    1    2    2    2    3    3    3     4     4     4   #
Gene2 avg. = row avg. = 2.5
[3,]    1    1    1    2    2    2    3    3    3     4     4     4   #
Gene3 avg. = row avg. = 2.5
[4,]    1    1    1    2    2    2    3    3    3     4     4     4   #
Gene4 avg. = row avg. = 2.5
[5,]    1    1    1    2    2    2    3    3    3     4     4     4   #
Gene5 avg. = row avg. = 2.5

#      |------- Treatment1 -------|  |-------- Treatment2 ---------| Tr1
avg. = (1+2)/2; Tr2 avg. = (3+4)/2
#      |-- Time1 --|  |-- Time2 --|  |-- Time1 --|     |-- Time2 --| Ti1
avg. = (1+3)/2; Ti2 avg. = (2+4)/2
#          (x3)           (x3)           (x3)              (x3)  

# This, above,is for illustration only; the actual data will have different
values for eah of the above
# [ 5x3x2x2 = 60 data individual, independent data points ]

> dim(x)
[1]  5 12

> n<-dim(x)[1]
> n
[1] 5

> dim(x)<-c(5,3,2,2)
> dim(x)
[1] 5 3 2 2

> genemeans <- apply(x, 1, mean, na.rm=TRUE)
> genemeans
[1] 2.5 2.5 2.5 2.5 2.5  # = row averages (5 Genes)

> timemeans <- apply(x, 3, mean, na.rm=TRUE)
> timemeans
[1] 2 3  # = Time1 avg  Time2 avg  (1+1+1+3+3+3)/6; (2+2+2+4+4+4)/6

> treatmeans <- apply(x, 4, mean, na.rm=TRUE)
> treatmeans
[1] 1.5 3.5  # Treatment1 avg  Treatment2 avg  (1+1+1+2+2+2)/6;
(3+3+3+4+4+4)/6

------------------------------------


--
View this message in context: http://r.789695.n4.nabble.com/Entering-data-into-a-multi-way-array-tp3862054p3886079.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list