Demo_Manuscript/zz_merge_helpers.R

# Variables ---------------------------------------------------------------

  demo_vars <- c(
    "id", "age", "sex", "race", "handedness", "weightkg", "weight_status"
  )

  swa_vars <- c(
    "id", "SWA_Time", "SWA_METs", "SWA_Intensity"
  )

# Functions ---------------------------------------------------------------

  renamer <- function(x, label) {

    stats::setNames(x, paste0(label, names(x))) %>%
      stats::setNames(
        ., gsub(paste0(label,"id"), "id", names(.))
      ) %>%
      stats::setNames(
        ., gsub(paste0(label, "Timestamp"), "Timestamp", names(.))
      ) %>%
      stats::setNames(
        ., gsub(paste0(label, "weightkg"), "weightkg", names(.))
      ) %>%
      stats::setNames(
        ., gsub(paste0(label, "Date"), "Date", names(.))
      )

  }

  AG_collapse <- function(AG) {

    AG %>%
      dplyr::group_by(
        id,
        Timestamp = lubridate::floor_date(
          Timestamp, "1 min"
        ), weightkg, Date
      ) %>%
      dplyr::summarise(
        n_sec = dplyr::n(),
        Axis1 = sum(Axis1),
        Axis2 = sum(Axis2),
        Axis3 = sum(Axis3),
        Steps = sum(Steps),
        is_nonwear = any(Choi_isNonWear),
        mean_METs = mean(METs),
        kcal = sum(METs*weightkg/3600)
      ) %>%
      data.frame(
        stringsAsFactors = FALSE, row.names = NULL
      ) %>%
      {.[.$n_sec == 60, setdiff(names(.), "n_sec")]} %T>%
      {stopifnot(nrow(.) == 1440)}

  }
PAHPLabResearch/FLASH documentation built on May 15, 2020, 7:08 p.m.