R/id_first_dict.R

Defines functions id_first_dict

Documented in id_first_dict

#' @title Relocate SUBJECT_ID to First Column of Data Dictionary
#' @description This utility function reorders the data dictionary so that SUBJECT_ID comes first.
#' @param DD.dict Data dictionary.
#' @return Updated data dictionary with SUBJECT_ID as first variable. 
#' @export
#' @details SUBJECT_ID is required to be the first column of the data set and first variable listed in the data dictionary. 
#' @examples
#' data(ExampleQ)
#' head(DD.dict.Q)
#' DD.dict.updated <- id_first_dict(DD.dict.Q)
#' head(DD.dict.updated)

id_first_dict <- function(DD.dict) {
  
  dict.id <- ('SUBJECT_ID' %in% DD.dict$VARNAME)
  
  stopifnot("ERROR: SUBJECT_ID variable missing from data dictinoary" = isTRUE(dict.id))
  
  id.no <- which(DD.dict$VARNAME=="SUBJECT_ID")
  DD.dict.updated <- DD.dict[c(id.no, 1:(id.no-1), (id.no+1):nrow(DD.dict)), ] 

  
  return(DD.dict.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.