假设以下数据:
dat <- data.frame(x1 = c(1, 2, 3, 4, 5),
x2 = c(2, 3, 4, 5, 6),
x3 = c(3, 4, 5, 6, 7),
x4 = c(7, 2, 3, 4, 5),
x5 = c(7, 2, 1, 4, 5))
进一步假设以下查找表:
lookup_positions <- data.frame(v1 = c(1,3,5),
v2 = c(1,2,5),
v3 = c(1,3,4),
v4 = c(2,3,5))
现在,我要执行以下操作:对于dat
我中的每一行,我都想要遍历中指定的所有组合lookup_positions
并计算中指定的dat
列位置的行总和lookup_positions
。
因此,对于其中的所有行,dat
我想计算的行总和dat[,c(1,3,5)]
,然后我要计算的行总和,dat[, c(1,2,5)]
依此类推。所以我基本上计算了4行总和。
我知道如何使用一个循环做到这一点的基础R,我现在也怎么做,在tidyverse方法的一个行总和,但不知道如何做到这一点的中提到的所有版本中lookup_positions
没有循环与tidyverse。
因此,预期结果将是:
x1 x2 x3 x4 x5 rowsum1 rowsum2 rowsum3 rowsum4
1 1 2 3 7 7 11 10 11 12
2 2 3 4 2 2 8 7 8 9
3 3 4 5 3 1 9 8 11 10
4 4 5 6 4 4 14 13 14 15
5 5 6 7 5 5 17 16 17 18
这是我在tidyverse中的lookup_positions之一中得到的。但是我在如何针对所有查找位置归纳这一问题上陷入了困境。
dat %>%
mutate(rowsum1 = apply(across(everything()), 1, function(x) sum(x[as.numeric(lookup_positions[1,])])))
我知道对于我的4个查找位置,我可以简单地执行复制粘贴并使用它完成操作,但是我的现实生活数据具有数百种查找位置组合。
一个dplyr
和purrr
选择可能是:
map2(.x = asplit(lookup_positions, 2),
.y = 1:ncol(lookup_positions),
~ dat %>%
mutate(!!paste0("rowsums", .y) := rowSums(select(., .x)))) %>%
reduce(full_join)
x1 x2 x3 x4 x5 rowsums1 rowsums2 rowsums3 rowsums4
1 1 2 3 7 7 11 10 11 12
2 2 3 4 2 2 8 7 8 9
3 3 4 5 3 1 9 8 11 10
4 4 5 6 4 4 14 13 14 15
5 5 6 7 5 5 17 16 17 18
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句