Cleaning up R code and adding RStudio project.

This commit is contained in:
Egeler, Paul W 2018-05-25 12:02:21 -04:00
parent f25319366c
commit cbc39e288e
3 changed files with 47 additions and 28 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.html *.html
.Rhistory .Rhistory
.Rproj.user

16
REDCapRITS.Rproj Normal file
View File

@ -0,0 +1,16 @@
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

View File

@ -1,7 +1,6 @@
# v0.0.0
#' Split REDCap repeating instruments table into multiple tables #' Split REDCap repeating instruments table into multiple tables
#' #'
#' This will take a raw data frame from REDCap and split it into a base table #' This will take a raw \code{data.frame} from REDCap and split it into a base table
#' and give individual tables for each repeating instrument. Metadata #' and give individual tables for each repeating instrument. Metadata
#' is used to determine which fields should be included in each resultant table. #' is used to determine which fields should be included in each resultant table.
#' #'
@ -45,43 +44,46 @@
#' @export #' @export
REDCap_split <- function(records, metadata) { REDCap_split <- function(records, metadata) {
# Check to see if there were any repeating instruments stopifnot(all(sapply(list(records,metadata), inherits, "data.frame")))
if (!any(names(records) == "redcap_repeat_instrument")) { # Check to see if there were any repeating instruments
warning("There are no repeating instruments.\n") if (!any(names(records) == "redcap_repeat_instrument")) {
return(list(records)) message("There are no repeating instruments in this data.")
} return(list(records))
# Clean the metadata }
metadata <- metadata[metadata["field_type"] != "descriptive", 1:4]
# Identify the subtables in the data # Clean the metadata
subtables <- unique(records["redcap_repeat_instrument"]) metadata <-
subtables <- subtables[subtables != ""] metadata[metadata$field_type != "descriptive", c("field_name", "form_name")]
# Split the table based on instrument # Identify the subtables in the data
out <- split.data.frame(records, records["redcap_repeat_instrument"]) subtables <- unique(records$redcap_repeat_instrument)
subtables <- subtables[subtables != ""]
# Delete the variables that are not relevant # Split the table based on instrument
for (i in names(out)) { out <- split.data.frame(records, records$redcap_repeat_instrument)
if (i == "") { # Delete the variables that are not relevant
for (i in names(out)) {
out[[which(names(out) == "")]] <- if (i == "") {
out[[which(names(out) == "")]][metadata[`!`(metadata[,2] %in% subtables), 1]]
} else { out[[which(names(out) == "")]] <-
out[[which(names(out) == "")]][metadata[!metadata[,2] %in% subtables, 1]]
out[[i]] <- } else {
out[[i]][c(names(records[1:3]),metadata[metadata[,2] == i, 1])]
} out[[i]] <-
out[[i]][c(names(records[1:3]),metadata[metadata[,2] == i, 1])]
} }
return(out) }
return(out)
} }