简体   繁体   English

dplyr mutate:传递变量列表以创建多个新变量

[英]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, C1A2, B2, C2A_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:一种dplyrpurrr可能性可能是:

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.

相关问题 dplyr mutate_at:使用重新编码创建新变量 - dplyr mutate_at: create new variables with recode 变异多个变量以创建多个新变量 - Mutate multiple variable to create multiple new variables 如何在dplyr mutate中将向量分配给多个变量 - How to assign vectors into multiple variables in dplyr mutate 使用 mutate 和 if/else if/else 语句创建多个新变量 - Using mutate with if/else if/else statements to create multiple new variables 我们真的可以在 dplyr 中将两组多个变量传递给 mutate - Can we actually pass two sets of multiple variables into mutate across in dplyr 使用 `dplyr::mutate()` 从向量中指定的名称创建几个新变量 - Using `dplyr::mutate()` to create several new variables from names specified in a vector 使用dplyr :: mutate创建一个新变量,并粘贴两个现有变量以供用户定义函数 - Create a new variable using dplyr::mutate and pasting two existing variables for user-defined function 在 dplyr::mutate 中使用名称向量和用于计算的向量创建几个新变量 - Create several new variables using a vector of names and a vector for computation within dplyr::mutate 在 dplyr 1.0.0 中使用 mutate() 和 cross() 从多个变量创建新变量 - creating new variables from multiple variable using mutate() and across() in dplyr 1.0.0 为什么我的 dplyr 使用 mutate 和 zoo 创建多个变量的代码非常慢? - Why is my dplyr code to create multiple variables using mutate and zoo incredibly slow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM