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"
So the data frame I created has two columns the first column has a date and the second column is a year after that column. Then the in_range checks if it is within a year of those dates and the Market Cap sums those true rows and gets the Market Cap. I want a data frame which gives me the market cap based on each date throughout the date data frame I created.
By using mutate() instead of summarise() you are making 3842 (number of dates in your range) copies of your sample dataset, so the result ends up with 57630 rows, is this what you want? maybe a sample of your desired output would be useful to understand your issue.
So once I get the market cap for each card I want to sum all of those market caps to get a total market cap. The total market cap is all I want. I want the total market cap for each date in the market cap dates to create an index tracking the change in total market cap over time.
Not entirely, have in mind that we don't have the same background context and domain specific knowledge as you, this is what I understand from what you have said.