check for each month, how many customers have bought a product within 7 days

i am a beginner in programming. I have a table in which each rows is an order (variables : id_customer and date). Its looks like this :

I want to set a function that calculates for each month the number of cutomers that have made an order within 7 days. How can i do this ?

can someone help me on this ?

thanks in advance !

you'll need the package lubridate to calculate which week is the given date at first. then you may use group_by and summarise function to summarise data according to week_of_year.

for example:

library(lubridate)

df1 <- data.frame(
  id_customer = sample(1:30,100,replace = T),
  date = sample(as_date('2019-1-1'):as_date('2019-6-30'),100,replace = T) %>% as_date
)

df1 %>% mutate(week_of_year = week(date)) %>% group_by(week_of_year) %>% 
  summarise(number_of_customers = sum(id_customer))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.