dplyr中两个矩阵列的行矩阵乘法

Rowwise matrix multiplication of two matrix columns in dplyr

我有一个小标题叫 to_rotate:

# A tibble: 32 x 3
   personID bun_xyz               mat_y            
      <int> <list>                <list>           
 1        1 <dbl[,3] [1,381 x 3]> <dbl[,3] [3 x 3]>
 2        2 <dbl[,3] [3,714 x 3]> <dbl[,3] [3 x 3]>
 3        3 <dbl[,3] [3,157 x 3]> <dbl[,3] [3 x 3]>
 4        4 <dbl[,3] [3,705 x 3]> <dbl[,3] [3 x 3]>
# ... with 28 more rows

我想对列表的两个列进行按行矩阵乘法,但我该怎么做呢?

我试过这个:

to_rotate %>%
    rowwise() %>%
    mutate(rotated = map2(bun_xyz, mat_y, ~ .x %*% .y))

但我收到错误:

Error: Mapped vectors must have consistent lengths:
* `.x` has length 4143
* `.y` has length 9

如果我只拿一行然后做"manually",一切都很好:

> rotated_1 = to_rotate$bun_xyz[[1]] %*% to_rotate$mat_y[[1]]
> head(rotated_1)

          [,1]      [,2]      [,3]
[1,] 0.4411675 0.7639250 0.3506840
[2,] 0.4438372 0.7625611 0.3518184
[3,] 0.4458833 0.7618375 0.3535549
[4,] 0.4452629 0.7607695 0.3538486
[5,] 0.4404777 0.7533813 0.3511128
[6,] 0.4398552 0.7514426 0.3508681

bun_xyz 是一些需要围绕 [=30= 旋转的 3D 坐标。mat_y 是可以执行此操作的矩阵)

删除 rowwise() 成功了:

to_rotate %>%
    mutate(rotated = map2(bun_xyz, mat_y, ~ .x %*% .y))

感谢 Mossa Nova 在评论中指出这一点。