-
Notifications
You must be signed in to change notification settings - Fork 7
/
check_rs_rsdtc_visit.R
executable file
·81 lines (71 loc) · 2.21 KB
/
check_rs_rsdtc_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#' @title Check missing RSDTC and VISIT
#'
#' @description This check looks for missing RSDTC or VISIT values when
#' RSORRES is not missing and RSSTAT not equal to "NOT DONE" in RS dataset
#' and returns a data frame. Only applies to assessments by investigator.
#'
#' @param RS Disease Response SDTM dataset with variables USUBJID, RSDTC,
#' RSORRES, VISIT, RSSTAT
#'
#' @return boolean value if check failed or passed with 'msg' attribute if the
#' test failed
#'
#' @export
#'
#' @importFrom dplyr %>% filter select
#'
#' @author Peggy Wen, Stella Banjo (HackR 2021)
#'
#' @examples
#'
#' RS <- data.frame(
#' USUBJID = 1:10,
#' RSDTC = 1:10,
#' RSORRES = "THING",
#' VISIT = "C1D1",
#' RSSTAT = 1:10,
#' RSEVAL = c("NA","","IRF","investigator",rep("INVESTIGATOR",6)),
#' stringsAsFactors=FALSE
#' )
#'
#' RS$RSDTC[1]=""
#' RS$RSDTC[2]="NA"
#' RS$RSDTC[3]=NA
#' RS$VISIT[3]=""
#' RS$VISIT[4]="NA"
#' RS$VISIT[5]=NA
#' check_rs_rsdtc_visit(RS)
#'
#' RS$RSORRES[1]=""
#' check_rs_rsdtc_visit(RS)
#'
#' RS$RSORRES[4] = "THING 1"
#' RS$RSORRES[5] = "THING 2"
#'
#' check_rs_rsdtc_visit(RS)
#'
#'
check_rs_rsdtc_visit <- function(RS){
###First check that required variables exist and return a message if they don't
if(RS %lacks_any% c("USUBJID", "RSDTC", "RSORRES", "VISIT", "RSSTAT")){
fail(lacks_msg(RS, c("USUBJID", "RSDTC", "RSORRES", "VISIT", "RSSTAT")))
} else{
if(RS %lacks_any% "RSEVAL"){
### Subset to only records with missing RSDTC or missing VISIT
mydf <- RS %>%
filter((is_sas_na(RSDTC) | is_sas_na(VISIT)) &!is_sas_na(RSORRES) & toupper(RSSTAT) != "NOT DONE") %>%
select(USUBJID, RSDTC, RSORRES, VISIT, RSSTAT)
}else{
mydf <- RS %>%
filter((is_sas_na(RSDTC) | is_sas_na(VISIT)) &!is_sas_na(RSORRES) & toupper(RSSTAT) != "NOT DONE" & (toupper(RSEVAL) == "INVESTIGATOR" | is_sas_na(RSEVAL)) ) %>%
select(USUBJID, RSDTC, RSORRES, VISIT, RSSTAT,RSEVAL)
}
rownames(mydf)=NULL
if (nrow(mydf)==0) {
pass()
} else{
fail(paste("There are",nrow(mydf),
"records with missing RSDTC or VISIT. "), mydf)
}
}
}