Hi Guys,
I am trying to use map_df to create a data frame rotating through different date ranges returning a value for them. Basically, I want to calculate the sum of the last year in sales with the last year being based on various dates. I am having trouble doing this. Here is what I have so far.
Sale_Date = seq(from = as.Date("2009-01-01"), to = as.Date("2010-03-02"), by= 'month')
blue_chips_market_cap_example <- data.frame(graded_title = "Mickey_Mantle", price = 1:15, Sale_Date = Sale_Date)
market_cap_late_dates <- (seq(from = as.Date("2009-01-01"), to = as.Date("2019-07-09"), by = 'day'))
market_cap_early_dates <- (seq(from = as.Date("2008-01-01"), to = as.Date("2018-07-08"), by = 'day'))
market_cap_dates <- as.data.frame(market_cap_early_dates) %>%
mutate(market_cap_late_dates)
#> Error in as.data.frame(market_cap_early_dates) %>% mutate(market_cap_late_dates): could not find function "%>%"
market_cap_dates
#> Error in eval(expr, envir, enclos): object 'market_cap_dates' not found
as.
blue_chips_market_cap_index_df <- map_df(market_cap_dates, function(market_cap_early_dates, market_cap_late_dates){
blue_chips_market_cap <- blue_chips %>%
mutate(in_range = ((Sale_Date >= as.Date("market_cap_early_dates")) & (Sale_Date <= "market_cap_late_dates"))) %>%
mutate(Market_Cap = sum(if_else(in_range == TRUE, price, 0))) %>%
distinct(.keep_all = TRUE)
mutate(Total_Market_Cap = sum(Market_Cap)) %>%
mutate(Index_Date = market_cap_late_dates)
})
#> Error in map_df(market_cap_dates, function(market_cap_early_dates, market_cap_late_dates) {: could not find function "map_df"