I have been playing with dplyr::mutate_at
to create new variables by applying the same function to some of the columns. When I name my function in the .funs
argument, the mutate call creates new columns with a suffix instead of replacing the existing ones, which is a cool option that I discovered in this thread.
df = data.frame(var1=1:2, var2=4:5, other=9)
df %>% mutate_at(vars(contains("var")), .funs=funs('sqrt'=sqrt))
#### var1 var2 other var1_sqrt var2_sqrt
#### 1 1 4 9 1.000000 2.000000
#### 2 2 5 9 1.414214 2.236068
However, I noticed that when the vars
argument used to point my columns returns only one column instead of several, the resulting new column drops the initial name: it gets named sqrt
instead of other_sqrt
here:
df %>% mutate_at(vars(contains("other")), .funs=funs('sqrt'=sqrt))
#### var1 var2 other sqrt
#### 1 1 4 9 3
#### 2 2 5 9 3
I would like to understand why this behaviour happens, and how to avoid it because I don't know in advance how many columns the contains()
will return.
EDIT: The newly created columns must inherit the original name of the original columns, plus the suffix 'sqrt' at the end.
Thanks