R/reorder_data.R

Defines functions reorder_data

Documented in reorder_data

#' @title Reorder Data Set Utility Function 
#' @description This utility function reorders the data set to match the data dictionary.
#' @param DD.dict Data dictionary.
#' @param DS.data Data set.
#' @return Updated data set with variables reordered to match the data dictionary. 
#' @export
#' @examples
#' data(ExampleN)
#' name_check(DD.dict.N, DS.data.N)
#' DS.data_updated <- reorder_data(DD.dict.N, DS.data.N)
#' name_check(DD.dict.N, DS.data_updated)

reorder_data <- function(DD.dict, DS.data) {
  
    # If the variable names match between the data dictionary and the data
    # then set the check to TRUE
    CHECK.name <- isTRUE(all.equal(names(DS.data), DD.dict$VARNAME))
    
    suppressWarnings(col_no <- which(names(DS.data) != DD.dict$VARNAME))
    Information <- bind_rows(Data=paste0("Data: ",names(DS.data)[col_no]), Dict=paste0("Dict: ",DD.dict$VARNAME[col_no]))
      
    if (!all(names(DS.data)==DD.dict$VARNAME) && all(sort(names(DS.data))==sort(DD.dict$VARNAME))) {
      Message <- c("CORRECTED ERROR: the variable names match between the data dictionary and the data, but they were in the wrong order. ***ALERT**** this function has temporarily reordered the dictionary to match the data so that you can continue working through the checks.")
      # Double << needed for scoping assignment to parent environment
      DS.data_updated <- DS.data[, match(DD.dict$VARNAME, names(DS.data))]
      Information$New.Data <- Information$Dict
    }
      
   if (!all(names(DS.data)==DD.dict$VARNAME) && !all(sort(names(DS.data))==sort(DD.dict$VARNAME))) {
      Message <- c("ERROR: the variable names DO NOT match between the data dictionary and the data, but you attempted to use the reorder data function. ***ALERT**** reordering FAILED. Please correct error before continuing.")
      Information <- "Function error"
    }
      
  return_to_user <- lst(Message, Information)
  print(return_to_user)
  return(DS.data_updated)
}

Try the dbGaPCheckup package in your browser

Any scripts or data that you put into this service are public.

dbGaPCheckup documentation built on Sept. 27, 2023, 5:06 p.m.