forked from COHHIO/r-hmis-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_ages.R
45 lines (35 loc) · 1.64 KB
/
04_ages.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
library(lubridate)
library(dplyr)
Client_Entry_Data <- Client_Entry_Data %>%
group_by(PersonalID) %>%
mutate(MostRecentEntry = max(EntryDate)) %>%
ungroup() %>%
mutate(ClientAge = ifelse(is.na(DOB),
NA,
ifelse(MostRecentEntry < Export_Data$ExportStartDate,
as.numeric(floor(as.period(interval(DOB, Export_Data$ExportStartDate)) / years(1))),
as.numeric(floor(as.period(interval(DOB, MostRecentEntry)) / years(1))))))
age_groups <- Client_Entry_Data %>%
group_by(PersonalID) %>%
summarise(ClientAge = first(ClientAge)) %>%
mutate(AgeGroup = case_when(
is.na(ClientAge) ~ NA_character_,
ClientAge < 5 ~ "Under 5",
ClientAge >= 5 & ClientAge <= 12 ~ "5 to 12",
ClientAge >= 13 & ClientAge <= 17 ~ "13 to 17",
ClientAge >= 18 & ClientAge <= 24 ~ "18 to 24",
ClientAge >= 25 & ClientAge <= 34 ~ "25 to 34",
ClientAge >= 35 & ClientAge <= 44 ~ "35 to 44",
ClientAge >= 45 & ClientAge <= 54 ~ "45 to 54",
ClientAge >= 55 & ClientAge <= 64 ~ "55 to 64",
ClientAge >= 65 ~ "65 and over",
TRUE ~ NA_character_
))
age_groups$AgeGroup <- factor(age_groups$AgeGroup,
levels = c("Under 5", "5 to 12", "13 to 17", "18 to 24",
"25 to 34", "35 to 44", "45 to 54", "55 to 64", "65 and over"))
Client_Entry_Data <- Client_Entry_Data %>%
left_join(age_groups %>% select(PersonalID, AgeGroup), by = "PersonalID")
age_group_counts <- Client_Entry_Data %>%
group_by(AgeGroup) %>%
summarise(DistinctPersonalIDCount = n_distinct(PersonalID))