I have the following code calculate output from scalar input and merge the results into a data frame together with the input. I tried to do the same thing with purrr and plyr, but got stuck. Can anyone provide some equivalent code?
library(data.table)
library(magrittr)
sigma = rep(2,5)
sim_ar1 = function(b1,sigma) b1 + sigma
data.table(b1 = seq(0.9,1,0.03)) %>%
.[,
data.frame(sigma, sim1 = sim_ar1(b1,sigma)),
by = b1]
Here is a solution using
tidyverse. The key is to useexpand.gridto create all the combination betweenb1andsigma, and then usemap2to apply thesim_ar1function to create thesim1column.dtshould look like the same as your example output fromdata.table.Update
In fact, since
sim_ar1is vectorized, there is no need to usemap2frompurrr.dplyralone is sufficient.