Home > Mobile >  Rolling sum subset by category by week and by month
Rolling sum subset by category by week and by month

Time:12-19

Merry Christmas, I've got a dataframe of this structure:

structure(list(Date = structure(c(13523, 13532, 13539, 13551, 
13565, 13567, 13579, 13588, 13600, 13607, 13616, 13628, 13637, 
13656, 13658, 13670, 13686, 13691, 13698, 13705, 13721, 13735, 
13768, 13770, 13783, 13789, 13797, 13811, 13819, 13824, 13838, 
13846, 13852, 13860), class = "Date"), Category = c("Type 1", 
"Type 2", "Type 1", "Type 1", "Type 1", "Type 2", "Type 1", "Type 3", 
"Type 1", "Type 1", "Type 2", "Type 1", "Type 1", "Type 1", "Type 2", 
"Type 1", "Type 3", "Type 1", "Type 1", "Type 1", "Type 1", "Type 2", 
"Type 1", "Type 3", "Type 1", "Type 1", "Type 1", "Type 1", "Type 2", 
"Type 1", "Type 1", "Type 1", "Type 3", "Type 2"), Value = c(2250, 
1200, 625, 2250, 1000, 2750, 2250, 2750, 950, 2000, 1100, 950, 
2250, 1000, 2500, 2250, 2500, 1000, 2250, 1200, 700, 2500, 2000, 
2500, 900, 2250, 1200, 925, 2500, 2250, 750, 2000, 2500, 950)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -34L), groups = structure(list(
    Date = structure(c(13523, 13532, 13539, 13551, 13565, 13567, 
    13579, 13588, 13600, 13607, 13616, 13628, 13637, 13656, 13658, 
    13670, 13686, 13691, 13698, 13705, 13721, 13735, 13768, 13770, 
    13783, 13789, 13797, 13811, 13819, 13824, 13838, 13846, 13852, 
    13860), class = "Date"), .rows = structure(list(1L, 2L, 3L, 
        4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
        16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
        27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -34L), .drop = TRUE))

I'd like to create two dataframes from this one. Both of these dataframes will be rolling sums of the 'Value' column by 'Category'. One dataframe being by week (and being labeled as week beginning date) and the other by month. I've had a look through similar questions, but unfortunately none seem to provide the necessary help.

Thanks for any assistance.

CodePudding user response:

To create rolling sums of the 'Value' column by 'Category' in R, you can use the group_by and summarize functions from the dplyr package. Here is an example of how the code might look for creating a rolling sum by week:

install and load dplyr package

install.packages("dplyr")
library(dplyr)

create new dataframe with rolling sum by week

 df_week <- df %>%
      group_by(Category, Week = format(Date, "%U")) %>%
      summarize(Rolling_Sum = sum(Value))

This code first installs and loads the dplyr package, then creates a new dataframe with a rolling sum of the 'Value' column by 'Category' and 'Week', where 'Week' is defined as the week number in the year using the format function. The new dataframe is created using the group_by and summarize functions from dplyr, which allow you to group the data by the desired variables and summarize the data using a specified function.

To create a rolling sum by month, you can use a similar approach, but group the data by month instead of week. Here is an example of how the code might look:

create new dataframe with rolling sum by month

df_month <- df %>%
  group_by(Category, Month = format(Date, "%m")) %>%
  summarize(Rolling_Sum = sum(Value))

This code creates a new dataframe with a rolling sum of the 'Value' column by 'Category' and 'Month', where 'Month' is defined as the month number in the year using the format function.

  • Related