generated from opensafely/research-template
/
process_data_kids.R
160 lines (128 loc) · 4.89 KB
/
process_data_kids.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
######################################################
# This script:
# - imports data extracted by the cohort extractor
# - combines all datasets into one
# - formats variables as appropriate
# - saves processed dataset(s)
#######################################################
# For running locally only #
#setwd("C:/Users/aschaffer/OneDrive - Nexus365/Documents/GitHub/opioids-covid-research")
#getwd()
# Import libraries #
library('tidyverse')
library('lubridate')
library('arrow')
library('here')
library('reshape2')
library('dplyr')
library('fs')
# Create directory
dir_create(here::here("output", "kids", "joined"), showWarnings = FALSE, recure = TRUE)
dir_create(here::here("output", "kids", "data"), showWarnings = FALSE, recure = TRUE)
# Custom functions
source(here("analysis", "lib", "custom_functions.R"))
###############################
# Prevalence datasets
###############################
# Combine data on any opioid prescribing
prevalence <- bind_rows(
read_csv(here::here("output", "kids", "data", "measure_opioid_all_any.csv")),
read_csv(here::here("output", "kids", "data", "measure_opioid_sex_any.csv")),
read_csv(here::here("output", "kids", "data", "measure_opioid_reg_any.csv")),
read_csv(here::here("output", "kids", "data", "measure_opioid_imd_any.csv")),
read_csv(here::here("output", "kids", "data", "measure_opioid_eth_any.csv"))
) %>%
mutate(date = as.Date(as.character(date), format = "%Y-%m-%d"),
# Sex
sex = fct_case_when(
sex == "F" ~ "Female",
sex == "M" ~ "Male",
TRUE ~ NA_character_),
# Ethnicity
ethnicity = ifelse(ethnicity == "", "Missing", ethnicity),
# IMD deciles
imdq10 = fct_case_when(
imdq10 == 0 ~ "Missing",
imdq10 == 1 ~ "1 most deprived",
imdq10 == 2 ~ "2",
imdq10 == 3 ~ "3",
imdq10 == 4 ~ "4",
imdq10 == 5 ~ "5",
imdq10 == 6 ~ "6",
imdq10 == 7 ~ "7",
imdq10 == 8 ~ "8",
imdq10 == 9 ~ "9",
imdq10 == 10 ~ "10 least deprived"
),
# Convert to integer to avoid scientific notation in csv
population = as.integer(population),
opioid_any = as.integer(opioid_any),
label = coalesce(region, imdq10, ethnicity, sex),
label = ifelse(is.na(label), "Total", label),
group = ifelse(!is.na(region), "Region",
ifelse(!is.na(imdq10), "IMD decile",
ifelse(!is.na(ethnicity), "Ethnicity",
ifelse(!is.na(sex), "Sex", "Total"))))) %>%
select(!c(region, imdq10, ethnicity, sex, value))
###############################
## Save as .csv
###############################
write.csv(prevalence, file = here::here("output", "kids", "joined", "final_ts_prev_kids.csv"))
###############################
# Read in data for tables
###############################
## Read in data before COVID and combine
# apr19 <- read_csv(here::here("output", "data", "input_2019-04-01.csv"))
# may19 <- read_csv(here::here("output", "data", "input_2019-05-01.csv")) %>%
# filter(!(patient_id %in% apr19$patient_id))
# jun19 <- read_csv(here::here("output", "data", "input_2019-06-01.csv")) %>%
# filter(!(patient_id %in% c(apr19$patient_id, may19$patient_id)))
#
# cohort_before <- rbind(apr19, may19, jun19) %>%
# select(!(c(opioid_any_date, hi_opioid_any_date))) %>%
# mutate(time = 0)
## Read in data and combine - people prescribed opioids during COVID and combine
apr20 <- read_csv(here::here("output", "kids", "data", "input_kids_2020-04-01.csv"))
may20 <- read_csv(here::here("output", "kids", "data", "input_kids_2020-05-01.csv")) %>%
filter(!patient_id %in% apr20$patient_id)
jun20 <- read_csv(here::here("output", "kids", "data", "input_kids_2020-06-01.csv")) %>%
filter(!patient_id %in% c(apr20$patient_id, may20$patient_id))
cohort <- rbind(apr20, may20, jun20) %>%
select(!(c(opioid_any_date)))
# Number check----
print(dim(cohort))
head(cohort)
#################################################
# Create base dataset for producing tables,
# including formatting variables as appropriate
#################################################
for_tables <-
cohort %>%
mutate(
# Sex
sex = fct_case_when(
sex == "F" ~ "Female",
sex == "M" ~ "Male",
TRUE ~ NA_character_),
# Ethnicity
ethnicity = ifelse(ethnicity == "", "Missing", ethnicity),
# IMD
imdq10 = fct_case_when(
imdq10 == 0 ~ "Missing",
imdq10 == 1 ~ "1 most deprived",
imdq10 == 2 ~ "2",
imdq10 == 3 ~ "3",
imdq10 == 4 ~ "4",
imdq10 == 5 ~ "5",
imdq10 == 6 ~ "6",
imdq10 == 7 ~ "7",
imdq10 == 8 ~ "8",
imdq10 == 9 ~ "9",
imdq10 == 10 ~ "10 least deprived",
TRUE ~ NA_character_
)
)
###############################
## Save as .csv
###############################
write.csv(for_tables, file = here::here("output", "kids", "joined", "final_for_tables_kids.csv"))