-
Notifications
You must be signed in to change notification settings - Fork 1
/
comedications.R
115 lines (100 loc) · 4.22 KB
/
comedications.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
# comedications
source('setup.R')
# beta-blockers, paracetamol, NASID, statins
# beta-blockers
bb_rms <- read_delim("lists_in/RMS/dl_beta_blockers_rms.txt",
"|", escape_double = FALSE,
trim_ws = TRUE, col_names = TRUE) %>%
mutate_at('read_term', list(~ str_remove(tolower(.), '^\\*'))) %>%
select(-Category)
bb_qof <- read_csv("lists_in/QOF/QOF_codes.csv") %>%
filter_at(vars(ends_with('id')), any_vars(. %in% 'LBB_COD'))
# bb_qof %>%
# select(contains('_v2_')) %>%
# filter_at(vars(ends_with('term')), any_vars(!is.na(.))) %>%
# print(n=nrow(.))
# # all v2 the same
# bb_qof %>%
# select(contains('_v3_')) %>%
# filter_at(vars(ends_with('term')), any_vars(!is.na(.))) %>%
# print(n=nrow(.))
# # all v3 the same
bb_qof <- bb_qof %>%
mutate(read_term = case_when(!is.na(v38_v2_term) ~ v38_v2_term,
!is.na(v38_v3_term) ~ v38_v3_term,
TRUE ~ NA_character_)) %>%
distinct(read_code, read_term) %>%
mutate_at('read_term', list(~ str_remove(tolower(.), '^\\*'))) %>%
mutate(QOF = 'yes') %>%
filter(read_code != 'bdf7.') # not sure why this is in the QOF list, maybe an error
bb <- bb_rms %>%
bind_rows(bb_qof) %>%
# so that QOF = yes comes first and are kept with distinct
arrange(read_code, QOF) %>%
distinct(read_code, .keep_all = TRUE) %>%
mutate(cat2 = 'beta-blocker')
# paracetamol
paracetamol <- read_delim("lists_in/RMS/dl_paracetamol_rms.txt",
"|", escape_double = FALSE,
trim_ws = TRUE, col_names = TRUE) %>%
mutate_at('read_term', list(~ str_remove(tolower(.), '^\\*'))) %>%
distinct(read_code, .keep_all = TRUE) %>%
mutate(cat2 = 'paracetamol')
# NSAID
NSAID <- read_delim("lists_in/RMS/dl_nsaid_rms.txt",
"|", escape_double = FALSE,
trim_ws = TRUE, col_names = TRUE) %>%
mutate_at('read_term', list(~ str_remove(tolower(.), '^\\*'))) %>%
distinct(read_code, .keep_all = TRUE) %>%
mutate(cat2 = 'NSAID')
# Statins
statin_qof <- read_csv("lists_in/QOF/QOF_codes.csv") %>%
filter_at(vars(ends_with('id')), any_vars(. %in% 'STAT_COD'))
# statin_qof %>%
# select(contains('_v2_')) %>%
# filter_at(vars(ends_with('term')), any_vars(!is.na(.))) %>%
# print(n=nrow(.))
# # all v2 the same
# statin_qof %>%
# select(contains('_v3_')) %>%
# filter_at(vars(ends_with('term')), any_vars(!is.na(.))) %>%
# print(n=nrow(.))
# # all v3 the same
statin_qof <- statin_qof %>%
mutate(read_term = case_when(!is.na(v38_v2_term) ~ v38_v2_term,
!is.na(v38_v3_term) ~ v38_v3_term,
TRUE ~ NA_character_)) %>%
distinct(read_code, read_term) %>%
mutate_at('read_term', list(~ str_remove(tolower(.), '^\\*'))) %>%
mutate(QOF = 'yes')
statin_rms <- read_delim("lists_in/RMS/dl_statins_rms.txt",
"|", escape_double = FALSE,
trim_ws = TRUE, col_names = TRUE) %>%
select(read_code, read_term = MX_PRODUCT_NAME) %>%
mutate_at('read_term', list(~ str_remove(tolower(.), '^\\*'))) %>%
filter(read_code != 'a222.') # I checked the browser and this is the wrong Read code
statin <- bind_rows(statin_qof, statin_rms) %>%
# so that QOF = yes comes first and are kept with distinct
arrange(read_code, QOF) %>%
distinct(read_code, .keep_all = TRUE) %>%
mutate(cat2 = 'statin')
# bind all
other_prescriptions <- bind_rows(bb, NSAID, paracetamol, statin) %>%
mutate(cat1 = 'other prescription') %>%
mutate_at('QOF', list(~ if_else(. %in% 'yes', ., 'no')))
# make sure all Read codes are 5 characters and fix if not
other_prescriptions %>%
filter(str_length(read_code)<5)
other_prescriptions <- other_prescriptions %>%
mutate_at('read_code', list(~ str_pad(., width=5, side='right', pad='.')))
# save list
write_csv(other_prescriptions,
path = file.path(opcrd_analysis_path, 'comedications.csv'))
write_csv(other_prescriptions %>%
arrange(cat2, read_code) %>%
select(Comedication = cat2,
`Read code` = read_code,
Term = read_term,
QOF) %>%
mutate_at('QOF', list(~ toupper(str_extract(., '.{1}')))),
path = 'lists_out/comedications.csv')