igora
April 18, 2022, 12:04pm
1
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))