简体   繁体   English

将归因于data.table的每个矩阵求和

[英]Sum each of the matrices resulting from grouping in data.table

For each group in by group of a data.table, I create a matrix. 对于data.table的每个分组,我创建一个矩阵。 I would like to add all these matrices into one resulting. 我想将所有这些矩阵加到一个结果中。 The following example illustrates better: 以下示例更好地说明了这一点:

set.seed(1)
library(data.table)
A <- data.table(A = letters[1:3], B = rnorm(3))
fun <- function(dt){ matrix(rnorm(9),nrow = 3, ncol = 3)  }
A[,fun(.SD), by = A]

The output from this is a column vector with all the matrix entries stacked. 它的输出是一个列向量,其中所有矩阵项都已堆叠。 I would like to recover the matrix form, or use another method. 我想恢复矩阵形式,或使用其他方法。

I would like to add all the matrices I get using by (so really, I wouldn't mind using by, or data.table, whichever gets me the answer): 我想添加我使用的所有矩阵(因此,我真的不介意使用by或data.table,无论哪个获取答案):

by(A, A$A, fun)

A$A: a
           [,1]       [,2]      [,3]
[1,] -0.6264538  1.5952808 0.4874291
[2,]  0.1836433  0.3295078 0.7383247
[3,] -0.8356286 -0.8204684 0.5757814
-------------------------------------------------------------------------------------- 
A$A: b
           [,1]       [,2]        [,3]
[1,] -0.3053884 -0.6212406 -0.04493361
[2,]  1.5117812 -2.2146999 -0.01619026
[3,]  0.3898432  1.1249309  0.94383621
-------------------------------------------------------------------------------------- 
A$A: c
          [,1]        [,2]        [,3]
[1,] 0.8212212  0.78213630  0.61982575
[2,] 0.5939013  0.07456498 -0.05612874
[3,] 0.9189774 -1.98935170 -0.15579551

The result of by is just a list of matrices. by的结果只是矩阵列表。 So you can simply add all the elements of the list (see How to sum a numeric list elements in R ): 因此,您可以简单地添加列表的所有元素(请参阅如何在R中对数字列表元素求和 ):

> set.seed(1)
> A <- data.table(A = letters[1:3], B = rnorm(3))
> fun <- function(dt){ matrix(rnorm(9),nrow = 3, ncol = 3)  }
> l <- by(A, A$A, fun)
> Reduce("+",l)
          [,1]      [,2]       [,3]
[1,]  1.756177 1.0623212 -0.9549196
[2,] -1.810627 0.6660057  1.6275324
[3,] -1.684889 1.3638221  1.7267622
> l[[1]] + l[[2]] + l[[3]]
          [,1]      [,2]       [,3]
[1,]  1.756177 1.0623212 -0.9549196
[2,] -1.810627 0.6660057  1.6275324
[3,] -1.684889 1.3638221  1.7267622

You can stay within data.table if you want by adding another level of list or .() nesting to j : 如果需要,可以通过向j添加另一级list.()嵌套,将其保留在data.table

A[,.(mats=.(fun(.SD))), by = A][, Reduce(`+`, mats)]
#          [,1]      [,2]       [,3]
#[1,]  1.756177 1.0623212 -0.9549196
#[2,] -1.810627 0.6660057  1.6275324
#[3,] -1.684889 1.3638221  1.7267622

Here is a tidyverse approach 这是一个tidyverse方法

library(tidyverse)
A %>%
     split(.$A) %>% 
     map(fun) %>%
     reduce(`+`)
#        [,1]       [,2]       [,3]
#[1,] -2.9614395  3.9622030  2.0717522
#[2,]  2.9163822  0.7773892  0.3030291
#[3,]  0.4163046 -1.1495702 -1.4320626

NOTE: The difference in output values is based on the seed selected ie 注意:输出值的差异基于所选的seed

set.seed(24)
Reduce(`+`, by(A, A$A, fun))
#            [,1]       [,2]       [,3]
#[1,]  0.06642572 -1.9509985 -0.6730669
#[2,] -0.26398712 -2.2912755 -0.8955920
#[3,]  0.94358370  0.3295733  0.6023412

set.seed(24)
A %>%
    split(.$A) %>%
    map(fun) %>% 
    reduce(`+`)
#          [,1]       [,2]       [,3]
#[1,]  0.06642572 -1.9509985 -0.6730669
#[2,] -0.26398712 -2.2912755 -0.8955920
#[3,]  0.94358370  0.3295733  0.6023412

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM