On Wed, Mar 14, 2012 at 09:10:49PM -0400, Axel Urbiz wrote:
> I'll appreciate your help on this. I have values stored in a list as in
> "mylist" below. I need to sum the values over all elements of the list
> aggregated by the names of the matrices.
> mylist <- list(matrix(c(0.2, 0.4), 1, 2, dimnames = list(NULL, c("1",
> "2"))),
>                     matrix(c(0.1, 0.5), 1, 2, dimnames = list(NULL, c("1",
> "3"))),
>                     matrix(c(0.3, 0.7), 1, 2, dimnames = list(NULL, c("2",
> "5"))))
> In this example, I'd like to get a single matix with the following values:
> 
> varname value
> 1       0.3   (i.e., 0.2 + 0.1)
> 2       0.7   (i.e., 0.4 + 0.3)
> 3       0.5   (i.e., 0.5)
> 5       0.7   (i.e., 0.7)

Hi.

If all the matrices have only one row as in the
example above, try the following.

  values <- unlist(mylist)
  cnames <- unlist(lapply(mylist, FUN=colnames))
  tapply(values, cnames, FUN=sum)

    1   2   3   5 
  0.3 0.7 0.5 0.7 

Hope this helps.

Petr Savicky.

______________________________________________
R-help@r-project.org 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.

Reply via email to