I've given it a little bit more thought and modified my solution to be a little more tidyversish. It's mostly for myself, but since it solves your problem all the same, I'll add it here for posterity
test <- structure(list(date = structure(c(1477872000, 1480464000, 1483142400,
1485820800, 1488240000, 1490918400, 1493510400, 1496188800, 1498780800,
1501459200, 1504137600, 1506729600, 1509408000, 1.512e+09, 1514678400,
1517356800, 1519776000, 1522454400, 1525046400, 1527724800, 1530316800
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), alpha = c(283L,
298L, 277L, 231L, 276L, 323L, 242L, 255L, 208L, 289L, 284L, 263L,
280L, 278L, 269L, 288L, 255L, 324L, 339L, 355L, 300L), alpha_mod = c(133L,
139L, 106L, 85L, 132L, 141L, 89L, 110L, 80L, 142L, 174L, 159L,
146L, 162L, 153L, 161L, 142L, 174L, 211L, 208L, 194L), beta = c(260L,
278L, 249L, 242L, 301L, 349L, 249L, 309L, 256L, 280L, 326L, 276L,
299L, 322L, 235L, 281L, 256L, 293L, 356L, 307L, 279L), beta_mod = c(102L,
119L, 92L, 107L, 119L, 126L, 108L, 132L, 89L, 141L, 199L, 148L,
161L, 160L, 125L, 159L, 137L, 139L, 208L, 177L, 162L), gam_ma = c(208L,
190L, 176L, 208L, 221L, 265L, 204L, 215L, 251L, 283L, 314L, 257L,
250L, 290L, 240L, 290L, 275L, 295L, 292L, 316L, 324L), gam_ma_mod = c(64L,
67L, 62L, 86L, 78L, 76L, 67L, 67L, 90L, 128L, 155L, 106L, 125L,
132L, 125L, 143L, 132L, 159L, 159L, 158L, 191L)), row.names = c(NA,
-21L), class = c("tbl_df", "tbl", "data.frame"))
library(tidyverse)
library(rlang)
#>
#> Attaching package: 'rlang'
#> The following objects are masked from 'package:purrr':
#>
#> %@%, %||%, as_function, flatten, flatten_chr, flatten_dbl,
#> flatten_int, flatten_lgl, invoke, list_along, modify, prepend,
#> rep_along, splice
nms <- test %>%
dplyr::select_at(vars(matches("_mod"))) %>%
names() %>%
gsub(pattern = "_mod", replacement = "", x = .)
add_unmod <- function(nm){
nm_sym <- rlang::sym(nm)
nm_mod <- rlang::sym(paste0(nm, "_mod"))
rlang::quo(!!nm_sym - !!nm_mod)
}
add_unmods <- purrr::map(nms, ~add_unmod(.x)) %>%
purrr::set_names(paste0(nms, "_unmod"))
test %>%
dplyr::mutate(!!!add_unmods)
#> # A tibble: 21 x 10
#> date alpha alpha_mod beta beta_mod gam_ma gam_ma_mod
#> <dttm> <int> <int> <int> <int> <int> <int>
#> 1 2016-10-31 00:00:00 283 133 260 102 208 64
#> 2 2016-11-30 00:00:00 298 139 278 119 190 67
#> 3 2016-12-31 00:00:00 277 106 249 92 176 62
#> 4 2017-01-31 00:00:00 231 85 242 107 208 86
#> 5 2017-02-28 00:00:00 276 132 301 119 221 78
#> 6 2017-03-31 00:00:00 323 141 349 126 265 76
#> 7 2017-04-30 00:00:00 242 89 249 108 204 67
#> 8 2017-05-31 00:00:00 255 110 309 132 215 67
#> 9 2017-06-30 00:00:00 208 80 256 89 251 90
#> 10 2017-07-31 00:00:00 289 142 280 141 283 128
#> # ... with 11 more rows, and 3 more variables: alpha_unmod <int>,
#> # beta_unmod <int>, gam_ma_unmod <int>
Created on 2018-07-14 by the reprex package (v0.2.0).