R/tocgmsdf.R

Defines functions tocgmsdf

Documented in tocgmsdf

#' Tidies the cgms datafiles generated from the preprocessing step.
#' Generates a time past start column that can be used to compare glucose over time
#' across different experiments. Generates the cgms.df file used in other functions
#'
#' @author Samuel Hamilton
#' @param cgms cgms file after preprocessing
#' @param census the file that contains all the subject info
#' @import dplyr lubridate
#' @keywords proeprocessing
#' @return tidied dataframe with timepast start column
#' @export
#' @examples cgms.df <- tocgmsdf(cgms,census)

tocgmsdf <- function(cgms,census) {

  cgms.df <- cgms %>%
    ###Join census for additional info
    distinct() %>%
    dplyr::left_join(census, by = c("subjno","projno","visit"))  %>%
    dplyr::mutate_at(c("projno","subjno","recordtype","id"),as.factor) %>%
    dplyr::mutate(time = lubridate::mdy_hm(time)) %>%
    dplyr::mutate(subj_id = paste(subjno,projno, sep = "_")) %>%
    dplyr::mutate(run_id = paste(subj_id,visit))  %>%
    #Calculate time_past_start, which standardizes experiments regardless of their start date
    dplyr::group_by(run_id) %>%
    dplyr::mutate(time_past_start = difftime(time,min(time),units = "days")) %>%
    ungroup(run_id)

  return(cgms.df)
}
hamsamilton/cgms.analysis documentation built on March 29, 2020, 12:57 a.m.