#' Rescales NAV to 1 at a specified date.
#'
#' \code{rescaleNav} rescales NAV in clean data (object returned by
#' \link{\code{getCleanData}}) to 1 at a specified base date.
#'
#' @export
#' @import dplyr
#'
#' @param cleanData Data frame with \code{date}, \code{id} and \code{nav}
#' columns. Ideally, an object returned by \link{\code{getCleanData}}.
#' @param baseDate String date convertible to date using \code{as.Date}
#' function, at which (or the next date existing in the \code{cleanData}) NAV
#' should be scaled to 1, and all others to their respective values.
#'
#' @return Data frame, same as \code{cleanData} but with rescaled \code{nav}
#' column.
rescaleNav <- function(cleanData, baseDate) {
baseDate <- as.Date(baseDate)
# Split cleanData by fund
lapply(split(cleanData, cleanData$id), function(fundData) {
baseFundDate<- min(fundData$date[fundData$date >= baseDate], na.rm = TRUE)
if (baseFundDate == Inf) { # data ends earlier than baseDate
baseFundDate<- max(fundData$date[fundData$date < baseDate], na.rm = TRUE)
}
baseFundNav <- fundData$nav[fundData$date == baseFundDate]
fundData$nav <- fundData$nav / baseFundNav
return(fundData)
}) %>%
data.table::rbindlist() %>%
as.data.frame() ->
rescaled
return(rescaled)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.