0

Calling a function from pmap is throwing an error as arguments are not being passed on their own

Tried creating lists of the parameters but this too has resulted in an error

library(tidyverse)
library(dplyr)

periods <- c(10,11,12)
redemption <- rep(100,3)
firstcallDate <- c("2014-01-01","2015-01-01","2016-01-01")
testdf <- data.frame(redemption, periods, firstcallDate)
testdf$firstcallDate <- as.Date(testdf$firstcallDate)
testdf$CallSch <- NA

CallScheduleGen <- function (redemption,periods,firstcallDate, ...) {
  Price <- rep(as.double(redemption),periods)
  Date <- seq(firstcallDate, by = "1 month", length = periods)
  callSch <- data.frame(Price, Date)
  return(callSch)   
}

testdf$CallSch <- pmap_dfr(testdf,CallScheduleGen)

I am expecting a dataframe to be created in each of the cells in the testdf dataframe. Pmap appears to pass input arguments to the functions as a list, rather than element-wise.

Can anyone suggest an approach as I need this to be vectorized rather than creating a loop?

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Try using `pmap` instead of `pmap_dfr` – Humpelstielzchen Jul 09 '19 at 12:13
  • thanks @Humpelstielzchen this helped with the output. So that others dont encounter this issue, pmap strips Date format: [link](https://github.com/tidyverse/purrr/issues/358) To get round this I changed the function: `Date <- seq(as.Date(firstcallDate,origin = "1970-01-01"), by = "1 month", length = periods)` – ruserb2122 Jul 09 '19 at 12:54

0 Answers0