R/spread_amount.R

#' Returns column names of specified class
#'
#' @param id identificator
#' @param amount amount to be spread in time
#' @param from starting date of period in which amount will be spread
#' @param to ending date of period
#' @param by increment of the sequence. Optional. See ‘Details’.
#' @return data.frame of values spreaded in time, with associated identificators
#' @details
#' by can be specified in several ways.
#' - A number, taken to be in days.
#' - A object of class difftime
#' - A character string, containing one of "day", "week", "month", "quarter" or "year". This can optionally be preceded by a (positive or negative) integer and a space, or followed by "s".
#' See seq.POSIXt for the details of "month".
#' @export
spreadAmount <- function(id,amount, from, to, by = "1 day") {
  dates <- mapply( seq.Date, from, to, by = by )
  fun <- function(amount, dates) rep(amount/length(dates), length(dates) )
  list <- mapply(  fun , amount , dates )
  for( i in 1:length(list) )
    list[[i]] <- data.frame( id = id[i], amount = list[[i]], date = dates[[i]] )

  df <- bind_rows(list)


}
gogonzo/oddsandsods documentation built on May 12, 2019, 1:35 a.m.