How to efficiently apply a function on a number of matrices - mean of columns

那年仲夏 提交于 2019-12-11 06:17:02

问题


So I am new to working with matrices and functions and I am trying to work out how to apply a function to calculate the column means to multiple matrices.

Here is some dummy martices:

A <- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3)
B <- matrix(c(9,8,7,6,5,4,3,2,1),nrow=3)

I have 13 large matrices all different variables, but they all have the same dimensions. I want to get the mean of the columns for each individual matrices. I have worked out how to do this for an individual matrix:

AA <- sapply(1:3, function(x) mean(A [,x], na.rm = TRUE))

But there is probably a more efficient way to apply this to all my matrices than writing this out a dozen times and get the individual outputs, i.e column means for each matrix separately? I have seen some work with using lists of matrices - is this the correct route to go? Apologies if this is a duplicate, i have tried to find a clear example with the correct answer to no avail (feel free to point me in the right direction).


回答1:


We keep the matrices in a list, use vapply to loop through the list and get the colMeans

vapply(list(A, B), colMeans, numeric(3))
#      [,1] [,2]
#[1,]    2    8
#[2,]    5    5
#[3,]    8    2

Or with aggregate

aggregate(do.call(rbind, list(A, B)), list(rep(1:2, each = 3)), FUN = mean)

Or using tidyverse

library(tidyverse)
list(A, B) %>%
      map(~ .x %>%
              as.data.frame %>%
               summarise_all(mean))
#[[1]]
#  V1 V2 V3
#1  2  5  8

#[[2]]
#  V1 V2 V3
#1  8  5  2

The tidyverse way can be chained for different purposes. It can also be a group by operation

list(A, B) %>%
    map_df(as.data.frame, .id = 'grp') %>%
    group_by(grp) %>%
    summarise_all(mean)
# A tibble: 2 x 4
#  grp      V1    V2    V3
#  <chr> <dbl> <dbl> <dbl>
#1 1         2     5     8
#2 2         8     5     2



回答2:


Create a list and then apply colMeans to each element in that list

lst <- list(A, B)
lapply(lst, colMeans)
#[[1]]
#[1] 2 5 8

#[[2]]
#[1] 8 5 2

I have seen some work with using lists of matrices - is this the correct route to go?

Yes, I'd say it's recommended for what you are trying to achieve.



来源:https://stackoverflow.com/questions/53749707/how-to-efficiently-apply-a-function-on-a-number-of-matrices-mean-of-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!