This commit is contained in:
AG Damsbo 2022-08-16 21:19:11 +02:00
parent 31b9a01498
commit 6c01fdf887

View File

@ -15,99 +15,33 @@ library(readxl)
library(dplyr)
## -----------------------------------------------------------------------------
## Data
## Data file load
## -----------------------------------------------------------------------------
setwd("/Volumes/Data/toorisky/")
dta_ls<-list()
dta_ls[1]<-read_dta("Alle apo Aarhus 2018.dta") %>% filter(VaskDiag==1) %>%
mutate(treatment=factor(case_when(trombolyse!=2&trombektomi!=2 ~ 0,
trombolyse==2|trombektomi==2 ~ 1)),
sex.n=factor(ifelse(as.integer(substr(cpr, start = 10, stop = 10)) %%2 == 0,
"female", "male"))) %>%
left_join(.,read_excel("2022-02-08_ddsc_dataexport.xlsx", sheet = "Patienter"),
by=c("ForloebID"="forloebid","cpr"="cpr"))
dta<-read_dta("Alle apo Aarhus 2018.dta") %>% # Defined dataset
filter(VaskDiag==1) %>%
mutate(treatment=factor(case_when(trombolyse!=2&trombektomi!=2 ~ 0,
trombolyse==2|trombektomi==2 ~ 1)),
sex.n=factor(ifelse(as.integer(substr(cpr, start = 10, stop = 10)) %%2 == 0,
"female", "male"))) %>%
left_join(.,read_excel("2022-02-08_ddsc_dataexport.xlsx", sheet = "Patienter"),
by=c("ForloebID"="forloebid"))%>%
left_join(.,read_excel("2022-02-08_ddsc_dataexport.xlsx", sheet = "3 mdr. opf."),
by=c("ForloebID"="ForloebID"))%>%
mutate(cpr=cpr.x,
ID=ID.x)%>%
dplyr::select(-starts_with("cpr."))%>%
dplyr::select(-starts_with("ID."))
dta_ls[2]<-read_excel("2022-02-08_ddsc_dataexport.xlsx", sheet = "3 mdr. opf.") # This excludes patients not treated and not candidates, but included by mistake in the register. Manually adjusted.
## Her mangler filter for kun at inkludere dem, fra baseline
setwd("/Users/au301842/nottreated/")
colnames(dta)<-tolower(colnames(dta))
## -----------------------------------------------------------------------------
## Data dictionary
## -----------------------------------------------------------------------------
icname<-colnames(read.csv("examlpe instrument.csv"))
dd<-data.frame(matrix(ncol = length(icname))) ## Data frame to collect all
colnames(dd)<-icname
## Tilpasses
## -----------------------------------------------------------------------------
## -----------------------------------------------------------------------------
for (i in 1:length(r_lup)){
dd_i<-data.frame(matrix(ncol = length(icname),nrow = ncol("[["(r_lup,i)))) ## Data frame to collect all
colnames(dd_i)<-icname ## for easier reading
## Variable names
dd_i[1]<-colnames("[["(r_lup,i))
## Form Name
dd_i[2]<-names(r_lup)[i]
## Field Type
# dd_i[4]<-ifelse(sapply(r_lup[[i]], class)=="factor","radio","text")
dd_i[4]<-"text"
## Field Label
## Using original attributes as field labels
fl<-lapply(r_lup[[i]], attr, "label")
for (j in 1:length(fl)){
fl[[j]]<-ifelse(is.null(fl[[j]]),
names(fl)[[j]],
fl[[j]])
## If no attributes, variable name is used as "placeholder"
}
dd_i[5]<-unlist(fl)
## Choices
# for (j in 1:ncol(r_lup[[i]])){
# if (is.factor(r_lup[[i]][[j]])){
# lvl<-levels(r_lup[[i]][[j]])
# lvl_ch<-paste("1,",lvl[1])
# for (k in 2:length(lvl)){
# lvl_ch<-c(paste0(lvl_ch," | ",k,", ",lvl[k]))
# }
# dd_i[j,6]<-lvl_ch
# }
# }
## Text Validation
## Only used for date and time data
# for (j in 1:ncol(r_lup[[i]])){
# dd_i[j,8]<-case_when(class(r_lup[[i]][[j]])[1]%in%c("POSIXct","POSIXt") ~"datetime_seconds_ymd",
# class(r_lup[[i]][[j]])[1]%in%c("Date") ~"date_ymd")
# }
## Merge all
dd<-rbind(dd,dd_i)
if (exp_out){
# dir.create(file.path("/Volumes/Data/REDCap/data",names(r_lup)[[i]]))
write.csv(r_lup[[i]],paste0("/Volumes/Data/REDCap/data/",names(r_lup)[[i]],".csv"),row.names = FALSE)
}
}
## -----------------------------------------------------------------------------
## REDCap pull
## Fix missing record_id's for upload
## -----------------------------------------------------------------------------
## REDCap pull with minimum data
token=names(suppressWarnings(read.csv("/Users/au301842/nottreated_redcap_token.csv",colClasses = "character")))
uri="https://redcap.au.dk/api/"
@ -115,11 +49,51 @@ library(REDCapR)
redcap <- redcap_read_oneshot(
redcap_uri = uri,
token = token
token = token,
fields = c("record_id","forloebid")
)$data
## Joining and adding record_id's
dta<-full_join(dta,redcap)
n_na<-length(dta$record_id[is.na(dta$record_id)])
n_id<-max(dta$record_id,na.rm=T)
# filter(!is.na(akut_ind))%>%
dta$record_id[is.na(dta$record_id)]<-(n_id+1):(n_id+n_na) # Simple math
## -----------------------------------------------------------------------------
## Data preparation
## Data dictionary
## -----------------------------------------------------------------------------
setwd("/Users/au301842/nottreated/")
icname<-colnames(read.csv("examlpe instrument.csv"))
dd<-data.frame(matrix(ncol = length(icname),
nrow=ncol(dta))) ## Data frame to collect all
colnames(dd)<-icname
## Variable names
dd[1]<-colnames(dta)
## Form Name
dd[2]<-"ddsc"
## Field Type
# dd_i[4]<-ifelse(sapply(r_lup[[i]], class)=="factor","radio","text")
dd[4]<-"text"
dd[5]<-colnames(dta)
## -----------------------------------------------------------------------------
## Instrument file
## -----------------------------------------------------------------------------
write.csv(dd,"ddsc_instrument.csv",row.names = FALSE,na="")
## -----------------------------------------------------------------------------
## Dataset export
## -----------------------------------------------------------------------------
write.csv(dta,"ddsc_dataset.csv",row.names = FALSE)