# [R] operations on a list

Fri Dec 9 08:59:35 CET 2005

```you could try :

trt1 <- do.call("rbind", lapply(listexp, function(x) {
zz <- aggregate(t(x), list(rep(1:3, each=2)), mean)[,-1]
zz <- as.matrix(zz)
sweep(zz[1:2,], 2, zz[3,], "-")[1,]}))

trt2 <- do.call("rbind", lapply(listexp, function(x) {
zz <- aggregate(t(x), list(rep(1:3, each=2)), mean)[,-1]
zz <- as.matrix(zz)
sweep(zz[1:2,], 2, zz[3,], "-")[2,]}))

expeffect <- list(trt1=trt1, trt2=trt2)

expeffect <- lapply(expeffect, function(x) {names(x) <- names(listexp) ; x})

szhan at uoguelph.ca a écrit :

>Hello, Everyone,
>I am sorry that my message got truncated due to wrong format.
>I hope it works now:
>
>Hello, R Users,
>I have a list (say listexp) of 10,000 elements, each of which consists of a
>matrix (5X6). It likes:
>\$"a"
>    trt1rep1    trt1rep2    trt2rep1    trt2rep2    ctlrep1    ctlrep2
>[1,]   50        54         98         89            40         45
>[2,]   60        65         76         79            34         43
>[3,]   86        83         34         45            38         34
>[4,]   67        78         88         98            45         41
>[5,]   55        59         77         88            56         66
>\$"b"
>   trt1rep1    trt1rep2    trt2rep1    trt2rep2    ctlrep1    ctlrep2
>[1,]   55        54         88         86            42         40
>[2,]   66        65         86         99            44         48
>[3,]   80        86         44         55            38         44
>[4,]   77        78         98         92            35         41
>[5,]   50        53         87         88            46         56
>
>.
>
>I want to perform some operations on the list and then got a new list(say
>expeffect) like this:
>1. first average two replicates for each row in the matrix shown above (like for
>treatment 1 using (trt1rep1+trt1rep2)/2) and get one value for each
>treatment/control
>2. then subtract average value of the control from the each treatment and get
>one value for the treatment effect for each row in the matrix shown above
>3. make a new list (say trteffect) with 2 elements (trt1 and trt2), each of
>which consists of a matrix (10,000X5)with a row name same with old list's
>element name, a column name corresponding to the row name of old list's matrix
>like:
>\$"trt1"
>     [,1]    [,2]    [,3]    [,4]     [,5]
>a    9.5     24       48.5    29.5     -4
>b   13.5     19.5     42      39.5      0.5
>.
>
>\$"trt2"
>     [,1]    [,2]    [,3]    [,4]     [,5]
>a    51      39      3.5     50       21.5
>b    46      46.5    8.5     57       30.5
>.
>
>Joshua
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help