-
Notifications
You must be signed in to change notification settings - Fork 7
/
check_ex_visit.R
executable file
·67 lines (56 loc) · 1.49 KB
/
check_ex_visit.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
#' @title Check for missing EX.VISIT
#'
#' @description This check looks missing EX.VISIT values when EX.EXOCCUR=Y (or EX.EXOCCUR doesn't exist)
#'
#' @param EX Exposure SDTM dataset with variables USUBJID,EXTRT,EXSTDTC,VISIT, and optional variable EXOCCUR
#'
#' @author Jen Chen
#'
#' @export
#'
#' @examples
#'
#' EX <- data.frame(
#' USUBJID = 1:3,
#' EXTRT = 1:3,
#' EXSTDTC = 1:3,
#' EXOCCUR = "Y",
#' VISIT = NA
#' )
#'
#' check_ex_visit(EX)
#'
#' EX$EXOCCUR=NULL
#'
#' check_ex_visit(EX)
#'
#' EX$VISIT=NULL
#'
#' check_ex_visit(EX)#
#'
check_ex_visit <- function(EX){
###First check that required variables exist and return a message if they don't
if(EX %lacks_any% c("USUBJID","EXTRT","EXSTDTC","VISIT")){
fail(lacks_msg(EX, c("USUBJID","EXTRT","EXSTDTC","VISIT")))
}else{
if(EX %has_all% c("EXOCCUR")){
### Subset EX to only records with missing VISIT
mydf = subset(EX,EX$EXOCCUR=='Y' & (is_sas_na(EX$VISIT)),c("USUBJID","EXTRT","EXSTDTC","EXOCCUR","VISIT"))
rownames(mydf)=NULL
}else{
mydf = subset(EX,is_sas_na(EX$VISIT),c("USUBJID","EXTRT","EXSTDTC","VISIT"))
rownames(mydf)=NULL
}
###Print to report
### Return message if no records with missing VISIT
if(nrow(mydf)==0){
pass()
### Return subset dataframe if there are records with missing VISIT
}else if(nrow(mydf)>0){
fail(
paste0("Total number of records is ",nrow(mydf),". "),
mydf
)
}
}
}