-
Notifications
You must be signed in to change notification settings - Fork 7
/
check_dm_dthfl_dthdtc.R
71 lines (63 loc) · 1.89 KB
/
check_dm_dthfl_dthdtc.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
#' @title Check that when DM.DTHFL is Y, DM.DTHDTC does not have a missing value, and vice versa
#'
#' @description This check is bi-directional for consistency of DM.DTHFL and DM.DTHDTC and returns a data
#' frame. Note there is a possible valid scenario for this issue if death date is truly unknown
#'
#' @param DM Demographics SDTM dataset with variables USUBJID,DTHFL,DTHDTC
#'
#' @return boolean value if check failed or passed with 'msg' attribute if the
#' test failed
#'
#' @export
#'
#' @importFrom dplyr %>% filter select
#'
#' @author Ross Farrugia
#'
#' @examples
#'
#' DM <- data.frame(
#' USUBJID = 1:7,
#' DTHFL = 1:7,
#' DTHDTC = 1:7
#' )
#'
#' DM$DTHFL[1] = ""
#' DM$DTHDTC[1] = "2020-01-01"
#' DM$DTHFL[2] = "N"
#' DM$DTHDTC[2] = "2020-01-01"
#' DM$DTHFL[3] = "Y"
#' DM$DTHDTC[3] = "2020-01-01"
#' DM$DTHFL[4] = "Y"
#' DM$DTHDTC[4] = ""
#' DM$DTHFL[5] = "N"
#' DM$DTHDTC[5] = ""
#' DM$DTHFL[6] = "Y"
#' DM$DTHDTC[6] = "2020"
#' DM$DTHFL[7] = ""
#' DM$DTHDTC[7] = ""
#' check_dm_dthfl_dthdtc(DM)
#'
#' DM$DTHFL <- NULL
#' DM$DTHDTC <- NULL
#' check_dm_dthfl_dthdtc(DM)
#'
check_dm_dthfl_dthdtc <- function(DM){
###First check that required variables exist and return a message if they don't
if(DM %lacks_any% c("USUBJID","DTHFL","DTHDTC")){
fail(lacks_msg(DM, c("USUBJID","DTHFL","DTHDTC")))
}else{
### Subset DM to only records with inconsistency between DTHFL and DTHDTC
mydf <- DM %>%
select("USUBJID","DTHFL","DTHDTC") %>%
filter((DM$DTHFL == "Y" & is_sas_na(DM$DTHDTC)) | (DM$DTHFL != "Y" & !is_sas_na(DM$DTHDTC)))
###Print to report
### Return message if no records with potential data issue
if(nrow(mydf)==0){
pass()
### Return subset dataframe if there are records with potential data issue
}else if(nrow(mydf)>0){
fail(paste("DM has ",nrow(mydf)," records with inconsistent values of DTHFL and DTHDTC. ",sep=""), mydf)
}
}
}