[R] how to ave this?

Dimitris Rizopoulos Dimitris.Rizopoulos at med.kuleuven.be
Sat Jun 23 10:08:46 CEST 2007


you could give a try to the following functions:

matFun <- function(lis, FUN){
     if(!is.list(lis) || !all(sapply(lis, is.matrix)))
         stop("'lis' must be a list containing 2-dimensional arrays")
     dims <- sapply(lis, dim)
     n <- dims[1, 1]
     p <- dims[2, 1]
     if(!all(n == dims[1, ]) || !all(p == dims[2, ]))
         stop("the matrices must have the same dimensions")
     mat <- matrix(unlist(lis), n * p, length(lis))
     matrix(apply(mat, 1, FUN), n, p)
}

matSums <- function(lis){
     if(!is.list(lis) || !all(sapply(lis, is.matrix)))
         stop("'lis' must be a list containing 2-dimensional arrays")
     dims <- sapply(lis, dim)
     n <- dims[1, 1]
     p <- dims[2, 1]
     if(!all(n == dims[1, ]) || !all(p == dims[2, ]))
         stop("the matrices must have the same dimensions")
     out <- array(data = 0, dim = c(n, p))
     for(i in seq(along = lis))
         out <- out + lis[[i]]
     out
}

################

n <- 6
p <- 5
lis <- list(matrix(rnorm(n * p), n, p), matrix(rnorm(n * p), n, p),
matrix(rnorm(n * p), n, p), matrix(rnorm(n * p), n, p),
matrix(rnorm(n * p), n, p))

matFun(lis, sum)
matSums(lis)


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
      http://www.student.kuleuven.be/~m0390867/dimitris.htm


Quoting Weiwei Shi <helprhelp at gmail.com>:

> one of my approaches is:
>
> x0 = sapply(mylist, cbind)
>
> and manipulate from x0 (x0[1:nrow(x0)/2, ] correponds to fc and the
> lower part is tt.
>
> but it is not neat way.
>
>
> On 6/22/07, Weiwei Shi <helprhelp at gmail.com> wrote:
>> Hi,
>>
>> I have a list that looks like this:
>> [[1]]
>>              fc          tt
>> 50   0.07526882 0.000000000
>> 100  0.09289617 0.000000000
>> 150  0.12359551 0.000000000
>>
>> [[2]]
>>              fc          tt
>> 50   0.02040816 0.000000000
>> 100  0.03626943 0.005025126
>> 150  0.05263158 0.010101010
>>
>> and I am wondering how to "average" it so that I have one matrix t0 at
>> the end, and t0[1,1] = (0.075..+0.0204..)/2
>>
>> Thanks,
>>
>> --
>> Weiwei Shi, Ph.D
>> Research Scientist
>> GeneGO, Inc.
>>
>> "Did you always know?"
>> "No, I did not. But I believed..."
>> ---Matrix III
>>
>
>
> --
> Weiwei Shi, Ph.D
> Research Scientist
> GeneGO, Inc.
>
> "Did you always know?"
> "No, I did not. But I believed..."
> ---Matrix III
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>
>



Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list