将每列 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()
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/