dplyr mutate multiple columns based on names in ve

2019-07-30 10:10发布

问题:

I want to multiply two columns with each other by using dplyr's mutate function.

But instead of writing a new line for each mutate conditions I would like to use the names of the columns stored in the vectors var1 and var2. For example in the end I want to have a additional column in my existing bankdata with the name result1 which contains the result by multiplying the columns cash and loans with each other. This shall be continued until 3 new columns have been created.

Reproducible code:

bankname <- c("Bank A", "Bank B", "Bank C", "Bank D", "Bank E")
bankid <- c(1, 2, 3, 4, 5)
year <- c(1881, 1881, 1881, 1881, 1881)
totass <- c(244789, 195755, 107736, 170600, 32000000)
cash <- c(7250, 10243, 13357, 35000, 351266)
bond <- c(20218, 185151, 177612, 20000, 314012)
loans <- c(29513, 2800, NA, 5000, NA)
bankdata <- data.frame(bankname, bankid, year, totass, cash, bond, loans)

Vectors var1 and var2 contain the column names I want to multiply (cash*loans, bond*cash, loans*bankid) and output is the name of the new column:

var1 <- c("cash", "bond", "loans")
var2 <- c("loans","cash", "bankid")
output <- c("result1", "result2", "result3")

I would like to do something similar like this:

bankdata %>%
  mutate_at(.funs = funs(output = var1*var2), vars(var1, var2))

bankdata %>%
  mutate_at(.funs = funs(result1 = cash*., result2 = bond*., result3 = loans*.), vars(var2))

回答1:

Using tidyeval approach, we build a function which can take strings as inputs then create new column. Note the use of rlang::sym and !! (bang bang).

After that we can use purrr::pmap_dfc to loop through var1, var2 to create new columns whose names supplied by output

library(tidyverse)

bankname <- c("Bank A", "Bank B", "Bank C", "Bank D", "Bank E")
bankid <- c(1, 2, 3, 4, 5)
year <- c(1881, 1881, 1881, 1881, 1881)
totass <- c(244789, 195755, 107736, 170600, 32000000)
cash <- c(7250, 10243, 13357, 35000, 351266)
bond <- c(20218, 185151, 177612, 20000, 314012)
loans <- c(29513, 2800, NA, 5000, NA)
bankdata <- data.frame(bankname, bankid, year, totass, cash, bond, loans)

originalNames <- names(bankdata)
var1   <- c("cash", "bond", "loans")
var2   <- c("loans","cash", "bankid")
output <- c("result1", "result2", "result3")

my_mutate <- function(df, var1, var2, output) {      
  var1   <- rlang::sym(var1)
  var2   <- rlang::sym(var2)
  output <- rlang::sym(output)

  df <- df %>% 
    mutate(!! output := !! var1 * !! var2)

  return(df)
}

# test
my_mutate(bankdata, var1[1], var2[1], output[1])

#>   bankname bankid year   totass   cash   bond loans   result1
#> 1   Bank A      1 1881   244789   7250  20218 29513 213969250
#> 2   Bank B      2 1881   195755  10243 185151  2800  28680400
#> 3   Bank C      3 1881   107736  13357 177612    NA        NA
#> 4   Bank D      4 1881   170600  35000  20000  5000 175000000
#> 5   Bank E      5 1881 32000000 351266 314012    NA        NA

# loop through 3 lists simultaneously 
# keep only original and result* columns
pmap_dfc(list(var1, var2, output), ~ my_mutate(bankdata, ..1, ..2, ..3)) %>% 
  select(!! originalNames, starts_with("result"))

#>   bankname bankid year   totass   cash   bond loans   result1      result2
#> 1   Bank A      1 1881   244789   7250  20218 29513 213969250    146580500
#> 2   Bank B      2 1881   195755  10243 185151  2800  28680400   1896501693
#> 3   Bank C      3 1881   107736  13357 177612    NA        NA   2372363484
#> 4   Bank D      4 1881   170600  35000  20000  5000 175000000    700000000
#> 5   Bank E      5 1881 32000000 351266 314012    NA        NA 110301739192
#>   result3
#> 1   29513
#> 2    5600
#> 3      NA
#> 4   20000
#> 5      NA

Created on 2018-04-18 by the reprex package (v0.2.0).