[英]dplyr mutate: pass list of variables to create multiple new variables
I'm trying to do something that intuitively feels straightforward but I can't figure it out.我正在尝试做一些直觉上感觉很简单但我无法弄清楚的事情。 I'm hoping to calculate delta's for a number of columns: I have columns
A1, B1, C1
and A2, B2, C2
and would like to create columns A_delta, B_delta, C_delta
by subtracting A2 - A1
etc.我希望计算多列的增量:我有
A1, B1, C1
和A2, B2, C2
列A_delta, B_delta, C_delta
通过减去A2 - A1
等来创建A_delta, B_delta, C_delta
列。
Here's what I thought I could do with dplyr
(using mtcars
as example):这是我认为我可以用
dplyr
做的dplyr
(以mtcars
为例):
# Create test data with changed columns
d.test <- mtcars %>%
rownames_to_column() %>%
mutate(mpg2 = mpg - 4,
cyl2 = cyl - 1)
# Calculate deltas & add as new columns
d.test %>% mutate(!!c("mpg_delta", "cyl_delta") := c(mpg2, cyl2) - c(mpg, cyl))
Clearly it doesn't work like this, but I cannot for the life of me figure out the right syntax.显然它不能像这样工作,但我终生无法找出正确的语法。 I've been reading up on using
purrr
but that seems applicable when trying to do different actions per row (like here dplyr mutate using variable columns ), not when trying to create multiple new columns...我一直在阅读有关使用
purrr
但这似乎适用于尝试对每行执行不同的操作(例如dplyr mutate using variable columns ),而不是尝试创建多个新列时...
Any pointers would be great!任何指针都会很棒!
One dplyr
and purrr
possibility could be:一种
dplyr
和purrr
可能性可能是:
map2_dfr(.x = d.test %>%
select(mpg2, cyl2),
.y = d.test %>%
select(mpg, cyl),
~ .x - .y) %>%
setNames(c("mpg_delta", "cyl_delta"))
mpg_delta cyl_delta
<dbl> <dbl>
1 -4 -1
2 -4 -1
3 -4 -1
4 -4 -1
5 -4 -1
6 -4 -1
7 -4 -1
8 -4 -1
9 -4 -1
10 -4 -1
Or:或者:
my_diff <- function(d, newvars, vars1, vars2) {
cmd <- unlist(pmap(list(newvars, vars1, vars2), ~exprs(!!..1 := !!..2 - !!..3)))
d %>%
mutate(!!!cmd)
}
d.test %>%
my_diff(vars(delta_mpg, delta_cyl), vars(mpg2, cyl2), vars(mpg, cyl))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.