generated from opensafely/research-template
/
ab_infection_all_transform_pre.R
168 lines (138 loc) · 6.48 KB
/
ab_infection_all_transform_pre.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
160
161
162
163
164
165
166
167
168
## This script is to transfer patinet/row --> ab_infection_times/ row
### every patient has 4 times of infection extraction per month
### variabless include:
### patient(id), age, sex, times(1-4), infection_date, infection_count, ab_infection(1-4)-binary flag
library("tidyverse")
library('dplyr')#conflict with plyr; load after plyr
library('lubridate')
rm(list=ls())
setwd(here::here("output", "measures"))
# variables names list
infect_date_4=paste0("indication_date_",rep(1:4))
ab_flag_4=paste0("ab_indication_date_",rep(1:4))
infect_category_4=paste0("indication_date_",rep(1:4),"_type")
df <- read_csv(('input_infection_all_pre_2018-10-01.csv.gz'),
col_types = cols_only(
indication_date_1 = col_date(format = ""),
indication_date_2 = col_date(format = ""),
indication_date_3 = col_date(format = ""),
indication_date_4 = col_date(format = ""),
ab_indication_date_1 = col_integer(),
ab_indication_date_2 = col_integer(),
ab_indication_date_3 = col_integer(),
ab_indication_date_4 = col_integer(),
indication_date_1_type = col_character(),
indication_date_2_type = col_character(),
indication_date_3_type = col_character(),
indication_date_4_type = col_character(),
age = col_integer(),
age_cat = col_character(),
sex = col_character(),
practice = col_integer(),
infection_count = col_integer(),
patient_id = col_integer()
),
na = character())
# filter all patients with infection record
df=df%>%filter(infection_count !=0)
df1=df%>%select(patient_id,age,sex,all_of(infect_date_4))
colnames(df1)[4:7]=paste0("time",rep(1:4))
df1.1=df1%>%gather(times,date,paste0("time",rep(1:4)))
rm(df1)
df2=df%>%select(patient_id,age,sex,all_of(ab_flag_4))
colnames(df2)[4:7]=paste0("time",rep(1:4))
df2.1=df2%>%gather(times,abflag,paste0("time",rep(1:4)))
rm(df2)
df3=df%>%select(patient_id,age,sex,all_of(infect_category_4))
colnames(df3)[4:7]=paste0("time",rep(1:4))
df3.1=df3%>%gather(times,infection,paste0("time",rep(1:4)))
rm(df3)
DF=merge(df1.1,df2.1,by=c("patient_id","age","sex","times"))
DF=merge(DF,df3.1,by=c("patient_id","age","sex","times"))
DF1=DF%>%filter(!is.na(date))
DF1$Date=as.Date("2018-10-01")
rm(df,DF,df1.1,df2.1,df3.1)
df <- read_csv(('input_infection_all_pre_2018-11-01.csv.gz'),
col_types = cols_only(
indication_date_1 = col_date(format = ""),
indication_date_2 = col_date(format = ""),
indication_date_3 = col_date(format = ""),
indication_date_4 = col_date(format = ""),
ab_indication_date_1 = col_integer(),
ab_indication_date_2 = col_integer(),
ab_indication_date_3 = col_integer(),
ab_indication_date_4 = col_integer(),
indication_date_1_type = col_character(),
indication_date_2_type = col_character(),
indication_date_3_type = col_character(),
indication_date_4_type = col_character(),
age = col_integer(),
age_cat = col_character(),
sex = col_character(),
practice = col_integer(),
infection_count = col_integer(),
patient_id = col_integer()
),
na = character())
# filter all patients with infection record
df=df%>%filter(infection_count !=0)
df1=df%>%select(patient_id,age,sex,all_of(infect_date_4))
colnames(df1)[4:7]=paste0("time",rep(1:4))
df1.1=df1%>%gather(times,date,paste0("time",rep(1:4)))
rm(df1)
df2=df%>%select(patient_id,age,sex,all_of(ab_flag_4))
colnames(df2)[4:7]=paste0("time",rep(1:4))
df2.1=df2%>%gather(times,abflag,paste0("time",rep(1:4)))
rm(df2)
df3=df%>%select(patient_id,age,sex,all_of(infect_category_4))
colnames(df3)[4:7]=paste0("time",rep(1:4))
df3.1=df3%>%gather(times,infection,paste0("time",rep(1:4)))
rm(df3)
DF=merge(df1.1,df2.1,by=c("patient_id","age","sex","times"))
DF=merge(DF,df3.1,by=c("patient_id","age","sex","times"))
DF2=DF%>%filter(!is.na(date))
DF2$Date=as.Date("2018-11-01")
rm(df,DF,df1.1,df2.1,df3.1)
df <- read_csv(('input_infection_all_pre_2018-12-01.csv.gz'),
col_types = cols_only(
indication_date_1 = col_date(format = ""),
indication_date_2 = col_date(format = ""),
indication_date_3 = col_date(format = ""),
indication_date_4 = col_date(format = ""),
ab_indication_date_1 = col_integer(),
ab_indication_date_2 = col_integer(),
ab_indication_date_3 = col_integer(),
ab_indication_date_4 = col_integer(),
indication_date_1_type = col_character(),
indication_date_2_type = col_character(),
indication_date_3_type = col_character(),
indication_date_4_type = col_character(),
age = col_integer(),
age_cat = col_character(),
sex = col_character(),
practice = col_integer(),
infection_count = col_integer(),
patient_id = col_integer()
),
na = character())
# filter all patients with infection record
df=df%>%filter(infection_count !=0)
df1=df%>%select(patient_id,age,sex,all_of(infect_date_4))
colnames(df1)[4:7]=paste0("time",rep(1:4))
df1.1=df1%>%gather(times,date,paste0("time",rep(1:4)))
rm(df1)
df2=df%>%select(patient_id,age,sex,all_of(ab_flag_4))
colnames(df2)[4:7]=paste0("time",rep(1:4))
df2.1=df2%>%gather(times,abflag,paste0("time",rep(1:4)))
rm(df2)
df3=df%>%select(patient_id,age,sex,all_of(infect_category_4))
colnames(df3)[4:7]=paste0("time",rep(1:4))
df3.1=df3%>%gather(times,infection,paste0("time",rep(1:4)))
rm(df3)
DF=merge(df1.1,df2.1,by=c("patient_id","age","sex","times"))
DF=merge(DF,df3.1,by=c("patient_id","age","sex","times"))
DF3=DF%>%filter(!is.na(date))
DF3$Date=as.Date("2018-12-01")
rm(df,DF,df1.1,df2.1,df3.1)
DF <- rbind(DF1,DF2,DF3)
saveRDS(DF, "infect_all_pre.rds")