I ran across an excellent SO post/answer here, which demonstrates that original variables can be retained with dplyr::mutate_at
while new variables are created with user-defined suffixes. The code runs as follows:
library(tidyverse)
dataframe <- data_frame(
helloo = c(1, 2, 3, 4, 5, 6),
ooooHH = c(1, 1, 1, 2, 2, 2),
ahaaa = c(200, 400, 120, 300, 100, 100)
)
dataframe %>%
mutate_at(
vars(contains("oo")),
.funs = funs(cat = ntile(., 2))
)
which produces
# A tibble: 3 x 5
helloo ooooHH ahaaa helloo_cat ooooHH_cat
<dbl> <dbl> <dbl> <int> <int>
1 1 1 200 1 1
2 2 1 400 1 1
3 3 1 120 2 2
However, it seems to be the case that if there is only one variable for vars
, this suffix scheme does not work as above: for example:
varlist <- c('ooooHH')
dataframe %>%
mutate_at(
vars(varlist),
.funs = funs(cat = ntile(., 2))
)
produces output as
# A tibble: 3 x 4
helloo ooooHH ahaaa cat
<dbl> <dbl> <dbl> <int>
1 1 1 200 1
2 2 1 400 1
3 3 1 120 2
I understand the rational---there is no need to distinguish the new variables if only one variable is mutated. However, I desire to have cat
column named as ooooHH_cat
for consistency, because I will do this multiple times and the dynamic column names that go into vars
will vary in length. In addition, I wish to preserve the result for when
varlist <- c('helloo', 'ooooHH')
Any suggestions?