-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_kanta_delivery.R
60 lines (42 loc) · 1.42 KB
/
filter_kanta_delivery.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
rm(list=ls())
gc()
library(data.table)
library(dplyr)
#_________________________________
# STATINS = starts with C10AA
ATC_regex <- '^C10AA'
#_________________________________
#### FUNCTION ####
extract_ATC_kanta <- function(from_path, to_path, filename, ATC_regex) {
print(filename)
t = Sys.time()
#FETCH DATA
df <- fread(paste0(from_path, filename))
df <- df %>%
#select columns of interest
select(PATIENT_ID, CREATION_DATE, DOC_GROUP_MD5HASH,
ATC_CODE, PRODUCT_CODE1,
DOSE_DISTRIBUTION, MED_EXCHANGED, RESEPTISTATUS) %>%
#filter for chosen drug
filter( grepl(ATC_regex, ATC_CODE)) %>%
#apply correct format to some variables
mutate(PRODUCT_CODE1 = if_else(PRODUCT_CODE1=="",NA_integer_,as.integer(PRODUCT_CODE1)) )
#SAVE RESULT
name <- strsplit(filename,".csv")[[1]][1]
new_name <- paste0(to_path, name,'.rds')
saveRDS(df, new_name)
print(Sys.time() - t)
gc()
}
#____________________________________
#### EXECUTE CODE ####
#NB: code takes approx 2hour
from_path <- "/data/processed_data/kela_kanta/"
to_path <- "/data/projects/project_mferro/kela_kanta_reliability/data/statins_deliveries/"
files <- system( paste0("ls ", from_path), intern = T)
files <- files[grepl('deliveries', files)]
for (file in files){
extract_ATC_kanta(from_path,to_path, file, ATC_regex)
}
rm(list=ls())
gc()