Let's say I have a data frame that looks like this:
user_id date weight
12345 2016-03-07 160
12345 2016-03-06 158
12345 2016-03-05 156
12345 2016-03-04 154
I would like to mutate this data frame by applying multiple functions to the weight
column, then saving the results of each application to a new column. The one caveat is that this function is parameterized, and I'd like to append the parameter value to the new column name. For example, if I use lag()
:
user_id date weight dweight_1 dweight_2 ...
12345 2016-03-07 160 NA NA
12345 2016-03-06 158 160 NA
12345 2016-03-05 156 158 160
12345 2016-03-04 154 156 158
where the first new column is the result of lag(weight, 1)
, the second from lag(weight, 2)
, and so on.
I tried following the standard evaluation approach proposed in dplyr's vignette on the subject, as well as suggestions from this SO question, but neither seem to address the wrinkle of a parameterized function (otherwise I'd just use funs()
!).
How can I tackle this problem?