r - 将数据帧的每一行乘以它的向量 R

将每列 x1:x10 与其各自的向量相乘的最简单方法是什么(如果可能的话使用 tidyverse)。 例如:新表的第一行是: age = "one", x1 = x1 * 1, x2 = x2 * 2, x3 = x3 * 9, x4 = x4 * 4...等

x <- data.frame(age = c("one", "two", "three", "four", "five","one", "two", "three", "four", "five"),
                replicate(10,sample(0:5,5,rep=TRUE)),
                time = c("one", "two", "three", "four", "five","one", "two", "three", "four", "five"),
                vector = c("1-2-9-4-5-1-5-6-1-2", 
                           "3-2-3-4-5-2-6-6-1-2", 
                           "1-2-4-4-2-4-5-4-2-1", 
                           "9-2-3-1-5-5-5-3-1-2", 
                           "1-1-3-4-5-1-5-6-3-2"))
   age X1 X2 X3 X4 X5 X6 X7 X8 X9 X10  time              vector
1    one  4  0  3  5  0  5  0  3  5   4   one 1-2-9-4-5-1-5-6-1-2
2    two  1  5  3  5  1  2  5  0  4   4   two 3-2-3-4-5-2-6-6-1-2
3  three  0  4  5  0  3  0  0  5  2   2 three 1-2-4-4-2-4-5-4-2-1
4   four  0  0  5  5  4  3  2  5  5   1  four 9-2-3-1-5-5-5-3-1-2
5   five  2  1  2  1  4  5  5  2  1   1  five 1-1-3-4-5-1-5-6-3-2
6    one  4  0  3  5  0  5  0  3  5   4   one 1-2-3-4-5-1-5-6-1-2
7    two  1  5  3  5  1  2  5  0  4   4   two 3-2-3-4-5-2-6-6-1-2
8  three  0  4  5  0  3  0  0  5  2   2 three 1-2-4-4-2-4-5-4-2-1
9   four  0  0  5  5  4  3  2  5  5   1  four 9-2-3-1-5-5-5-3-1-2
10  five  2  1  2  1  4  5  5  2  1   1  five 1-1-3-4-5-1-5-6-3-9

我可以通过将最后一列拆分为多列然后将每一列相乘来实现,但我正在寻找一种更快的方法

谢谢

最佳答案

您可以使用 dplyr 强大的 cur_data()

在单个 mutate 语句中执行此操作
set.seed(2021)
x <- data.frame(age = c("one", "two", "three", "four", "five","one", "two", "three", "four", "five"),
                replicate(10,sample(0:5,5,rep=TRUE)),
                time = c("one", "two", "three", "four", "five","one", "two", "three", "four", "five"),
                vector = c("1-2-9-4-5-1-5-6-1-2", 
                           "3-2-3-4-5-2-6-6-1-2", 
                           "1-2-4-4-2-4-5-4-2-1", 
                           "9-2-3-1-5-5-5-3-1-2", 
                           "1-1-3-4-5-1-5-6-3-2"))

library(tidyverse)

x %>% mutate(select(cur_data(), starts_with('X')) * t(map_dfc(strsplit(vector, '-'), as.numeric)))

#>      age X1 X2 X3 X4 X5 X6 X7 X8 X9 X10  time              vector
#> 1    one  5 10 36  4 25  1 20 12  1   0   one 1-2-9-4-5-1-5-6-1-2
#> 2    two 15 10  0  8  5  4  0  6  4   0   two 3-2-3-4-5-2-6-6-1-2
#> 3  three  1  4 12 12  6 12 25 20 10   5 three 1-2-4-4-2-4-5-4-2-1
#> 4   four 27 10  6  4 20 20  5 15  2   8  four 9-2-3-1-5-5-5-3-1-2
#> 5   five  3  5  9  8 25  5 10 30  3   6  five 1-1-3-4-5-1-5-6-3-2
#> 6    one  5 10 36  4 25  1 20 12  1   0   one 1-2-9-4-5-1-5-6-1-2
#> 7    two 15 10  0  8  5  4  0  6  4   0   two 3-2-3-4-5-2-6-6-1-2
#> 8  three  1  4 12 12  6 12 25 20 10   5 three 1-2-4-4-2-4-5-4-2-1
#> 9   four 27 10  6  4 20 20  5 15  2   8  four 9-2-3-1-5-5-5-3-1-2
#> 10  five  3  5  9  8 25  5 10 30  3   6  five 1-1-3-4-5-1-5-6-3-2

甚至按照 G.Grothendieck 的建议使用 across(这将消除 cur_data()

的使用)
x %>% mutate(across(starts_with('X')) * t(map_dfc(strsplit(vector, '-'), as.numeric)))

https://stackoverflow.com/questions/67945385/

相关文章:

docker - docker compose up 后后端到 redis 连接被拒绝

c - 分拣技术 C

vuejs3 - react 对象未在模板 Vue3 Composition API 上更新

scala - 推特 future 封锁

html - 如何使整个 HTML 日期字段可点击?

javascript - 计算数组中每个元素的频率 - javascript

sql - 有没有一种方法可以不使用 FOR 循环来创建虚拟记录?

r - 从单个表中查找多列

next.js - NextJS 将类从页面传递到布局组件

javascript - Target 的 Toggled ClassList 上的 CSS Tra