I would like to mutate new columns onto a dataframe using a function where the inputs to the function come from a vector. Like this:
library(tidyverse)
# sessionInfo()
# R version 4.3.2 (2023-10-31)
set.seed(123) # For reproducibility
mydf <- data.frame(user_id = 1:100) |>
expand_grid(data.frame(day = 1:365)) |>
mutate(logins = floor(abs(rnorm(1,10,10))))
lambdas <- c(0.01, 0.05, 0.1)
mydf <- mydf |>
mutate(
lambda_logins_01 = logins * exp(-lambdas[1] * day),
lambda_logins_05 = logins * exp(-lambdas[2] * day),
lambda_logins_1 = logins * exp(-lambdas[3] * day),
)
Except, instead of writing out "mutate( lambda_logins_01 ..." I wanted to do it more elegantly using something like map.
I would like to use the native pipe and refer to cur_data() or equivilent, as opposed to refering to mydf, i.e. I want to use the pipe in a traditional sense, referring to the current state of data I'm working on.
Desired result would be similar as in my example code, with the new columns named based on the values of lambda, except I wouldnt have to write out each mutate line manually.