Skip to content

Commit

Permalink
#11 updated clean_REDCap() to work with all datetime formats
Browse files Browse the repository at this point in the history
  • Loading branch information
pgstevenson committed Jul 7, 2021
1 parent 19fc1e9 commit fa18e27
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ importFrom(igraph,layout_with_fr)
importFrom(janitor,excel_numeric_to_date)
importFrom(kableExtra,cell_spec)
importFrom(labelled,set_variable_labels)
importFrom(lubridate,dmy)
importFrom(lubridate,dmy_hm)
importFrom(lubridate,dmy_hms)
importFrom(lubridate,hm)
importFrom(lubridate,int_end)
importFrom(lubridate,int_overlaps)
importFrom(lubridate,int_start)
importFrom(lubridate,interval)
importFrom(lubridate,mdy)
importFrom(lubridate,mdy_hm)
importFrom(lubridate,mdy_hms)
importFrom(lubridate,ms)
importFrom(lubridate,seconds)
importFrom(lubridate,ymd)
Expand Down
17 changes: 13 additions & 4 deletions R/clean_REDCap.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ yesno_vars <- function(d) {
#' @importFrom dplyr mutate across
#' @importFrom magrittr `%>%`
#' @importFrom tidyselect starts_with
#' @importFrom lubridate ymd ymd_hm ymd_hms hm ms
#' @importFrom lubridate ymd mdy dmy ymd_hm mdy_hm dmy_hm ymd_hms mdy_hms dmy_hms hm ms
#' @importFrom janitor excel_numeric_to_date
#'
#' @examples
Expand Down Expand Up @@ -357,9 +357,18 @@ clean_REDCap <- function(d, dict, numeric_date = FALSE, yesno_to_bool = FALSE, q
d <- mutate(d, across(dict[grepl("^date.*$", dict$`Text Validation Type OR Show Slider Number`, perl = TRUE),]$`Variable / Field Name`, ~excel_numeric_to_date(as.numeric(.))))
} else {
d <- mutate(d,
across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^date_"), isTRUE),]$`Variable / Field Name`, ymd, quiet = quiet),
across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^datetime_(?!seconds_)"), isTRUE),]$`Variable / Field Name`, ymd_hm, quiet = quiet),
across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^datetime_seconds_"), isTRUE),]$`Variable / Field Name`, ymd_hms, quiet = quiet),
across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^date_ymd"), isTRUE),]$`Variable / Field Name`, ymd, quiet = quiet),
across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^date_mdy"), isTRUE),]$`Variable / Field Name`, mdy, quiet = quiet),
across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^date_dmy"), isTRUE),]$`Variable / Field Name`, dmy, quiet = quiet),

across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^datetime_ymd"), isTRUE),]$`Variable / Field Name`, ymd_hm, quiet = quiet),
across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^datetime_mdy"), isTRUE),]$`Variable / Field Name`, mdy_hm, quiet = quiet),
across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^datetime_dmy"), isTRUE),]$`Variable / Field Name`, dmy_hm, quiet = quiet),

across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^datetime_seconds_ymd"), isTRUE),]$`Variable / Field Name`, ymd_hms, quiet = quiet),
across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^datetime_seconds_mdy"), isTRUE),]$`Variable / Field Name`, mdy_hms, quiet = quiet),
across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^datetime_seconds_dmy"), isTRUE),]$`Variable / Field Name`, dmy_hms, quiet = quiet),

across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^time$"), isTRUE),]$`Variable / Field Name`, hm, quiet = quiet),
across(dict[map_lgl(str_detect(dict$`Text Validation Type OR Show Slider Number`, "^time_mm_ss$"), isTRUE),]$`Variable / Field Name`, ms, quiet = quiet))
}
Expand Down
4 changes: 2 additions & 2 deletions data-raw/REDCap_test_data.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
id1,yesno1,checkbox1___1,checkbox1___2,dropdown1,radio1,date_dmy1,date_mdy1,date_ymd1,datetime_dmy1,datetime_mdy1,datetime_ymd1,datetime_seconds_dmy1,datetime_seconds_mdy1,datetime_seconds_ymd1,integer1,number1,number_comma_decimal1,number_1dp1,number_1dp_comma_decimal1
1,0,0,1,1,1,1985-02-16,1985-02-16,1985-02-16,1985-02-16 12:01 AM,1985-02-16 12:01 AM,1985-02-16 12:01 AM,1985-02-16 12:01:01 AM,1985-02-16 12:01:01 AM,1985-02-16 12:01:01 AM,1,1.5,"1,5",1.5,"1,5"
2,1,1,0,2,2,1985-02-16,1985-02-16,1985-02-16,1985-02-16 12:01 PM,1985-02-16 12:01 PM,1985-02-16 12:01 PM,1985-02-16 12:01:01 PM,1985-02-16 12:01:01 PM,1985-02-16 12:01:01 PM,1,1.5,"1,5",1.5,"1,5"
1,0,0,1,1,1,16/02/1985,02/16/1985,1985/02/16,16/02/1985 12:01 AM,02/16/1985 12:01 AM,1985/02/16 12:01 AM,16/02/1985 12:01:01 AM,02/16/1985 12:01:01 AM,1985/02/16 12:01:01 AM,1,1.5,"1,5",1.5,"1,5"
2,1,1,0,2,2,16/02/1985,02/16/1985,1985/02/16,16/02/1985 12:01 PM,02/16/1985 12:01 PM,1985/02/16 12:01 PM,16/02/1985 12:01:01 PM,02/16/1985 12:01:01 PM,1985/02/16 12:01:01 PM,1,1.5,"1,5",1.5,"1,5"
Binary file modified tests/resources/clean_REDCap.RData
Binary file not shown.

0 comments on commit fa18e27

Please sign in to comment.