Switch to unified view

a b/RadETL/R/radiologyOccurrenceDateTime_OccurrenceTable.R
1
#' 'radiologyOccurrenceDateTime'
2
#'
3
#' radiologyOccurrenceDateTime function indicates date and time of each occurrences are taken
4
#'
5
#'
6
#' @param DICOMList you can put it like this and then run the function : DICOMList<-DICOMHeaderList(DICOMFolderPath)
7
#' @import dplyr
8
#' @importFrom magrittr "%>%"
9
#'
10
#'
11
#' @return A dataframe indicating date and time of each occurrences are taken
12
#' @examples
13
#' DICOMList<-DICOMHeaderList(DICOMFolderPath)
14
#' radiologyOccurrenceDateTime(DICOMList)
15
#' @export
16
17
#radiologyOccurrenceDateTime
18
19
radiologyOccurrenceDateTime<-function(DICOMList){
20
    radiologyOccurrenceDateTime<-lapply(DICOMList, function(x){
21
        studyDate<-as.character(x[[1]] %>% filter(name %in% c('StudyDate')) %>% select(value))
22
        studyTime<-as.character(x[[1]] %>% filter(name %in% c('StudyTime')) %>% select(value))
23
        if(studyDate=="character(0)" | studyDate==""){
24
            studyDate='NA'
25
        }
26
        if(studyTime=="character(0)" | studyTime==""){
27
            studyTime='NA'
28
        }
29
        studyDateTime<-paste(studyDate, studyTime, sep = '')
30
        studyDateTime<-gsub("NA", '', studyDateTime)
31
        studyDateTime<-ifelse(is.na(as.POSIXct(studyDateTime, format = '%Y%m%d%H%M%S',origin = "1970-01-01",tz ="UTC"))==F, as.character(as.POSIXct(studyDateTime, format = '%Y%m%d%H%M%S',origin = "1970-01-01",tz ="UTC")), ifelse(is.na(as.POSIXct(studyDateTime, format = '%Y%m%d',origin = "1970-01-01",tz ="UTC"))==F, as.character(as.POSIXct(studyDate, format = '%Y%m%d',origin = "1970-01-01",tz ="UTC")), 'NA'))
32
        return(studyDateTime)
33
    })
34
    radiologyOccurrenceDateTime<-do.call(rbind, radiologyOccurrenceDateTime)
35
    radiologyOccurrenceDateTime<-as.data.frame(radiologyOccurrenceDateTime)
36
    colnames(radiologyOccurrenceDateTime)<-'studyDateTime'
37
    radiologyOccurrenceDateTime<-cbind(radiologyOccurrenceId(DICOMList), radiologyOccurrenceDateTime)
38
    radiologyOccurrenceDateTime<-as.data.frame(radiologyOccurrenceDateTime %>% group_by(radiologyOccurrenceId) %>% distinct(studyDateTime))
39
    radiologyOccurrenceDateTime<-split(radiologyOccurrenceDateTime, radiologyOccurrenceDateTime$radiologyOccurrenceId)
40
    radiologyOccurrenceDateTime<-lapply(radiologyOccurrenceDateTime, function(x){
41
        as.data.frame(x[c(1),])
42
    })
43
    radiologyOccurrenceDateTime<-data.frame(do.call(rbind, radiologyOccurrenceDateTime), row.names = NULL)
44
    return(radiologyOccurrenceDateTime)
45
}