Last active
February 11, 2022 15:14
-
-
Save Lextuga007/ed354eab45924562a3183499b7ee5547 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(tidyverse) | |
| library(lubridate) | |
| # Fix the random records according to this 'seed' so everytime the sequence is | |
| # generated it is always the same | |
| set.seed(123) | |
| # create data and repeat 34 x 3 = 102 times | |
| data <- data.frame( | |
| date = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by = "day"), 102), | |
| visits = floor(runif(102, min = 0, max = 101)) | |
| ) | |
| # add column with month year column | |
| counted_data <- data %>% | |
| mutate(month_year = lubridate::floor_date(date, "month")) %>% | |
| group_by(month_year) %>% | |
| summarise(frequency = n()) | |
| # If the reporting date was actually from 1998/12/01 to 2000/01/01 and so are missing | |
| # in this table as there were no records, this can be added into the code by using | |
| # another created date table and joining | |
| date_of_report <- data.frame( | |
| date = seq(as.Date('1998/12/01'), as.Date('2000/01/01'), by = "month")) | |
| completed_data <- date_of_report %>% | |
| left_join(data %>% | |
| mutate(month_year = lubridate::floor_date(date, "month")) %>% | |
| group_by(month_year) %>% | |
| summarise(frequency = n()), by = c("date" = "month_year")) %>% | |
| replace_na(list(frequency = 0)) # fills in 0 where it says NA in frequency | |
| # Actually required date diff to see if the time was between 0-6 months | |
| # interval_period = interval(dob, today) | |
| # full_year = interval_period %/% months(1) | |
| # https://stackoverflow.com/questions/32312925/time-difference-in-years-with-lubridate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment