data-raw/Demographic/sample_code/sample_code.R

# THIS FILE CONTAINS SAMPLE CODE FOR PROCESSING THE DEMOGRAPHIC DATA, BEGINNING
# FROM THE FORMAT SHOWN IN `SAMPLE_FILE.XLSX`

# Read Data ---------------------------------------------------------------

  rm(list = ls()) # Clear memory
  library(magrittr) # Attach magrittr package (makes code more readable)

  flash_demo <-
    "data-raw/Demographic/sample_code/sample_file.xlsx" %>%
    readxl::read_excel(.) %>%
    data.frame(stringsAsFactors = FALSE)

# Format Data -------------------------------------------------------------

  ## Variable names

    names(flash_demo) %<>%
      gsub("\\.+", "_", .) %>%
      gsub("_$", "", .) %>%
      gsub("^Gender$", "Sex", .) %>%
      tolower(.)

  ## Age & Sex

    flash_demo$age %<>% factor(.)
    flash_demo$sex %<>% factor(c("M", "F"))

  ## Race/Ethnicity

    races <- c(
      "White_nonHispanic", "White_Hispanic", "Black_nonHispanic",
      "Black_Hispanic", "Asian", "American_Indian_Native_American",
      "Other"
    )

    flash_demo$race %<>%
      races[.] %>%
      factor(races)

  ## Activity Monitor History

    monitor <- c(
      "Current_user", "Yes_past_6mo_not_current",
      "Yes_past_>6mo_not_current", "Never"
    )

    flash_demo$activity_monitor_before %<>%
      monitor[.] %>%
      factor(levels = monitor)

  ## Questions

    question_names <- c(
      "q1","q2","q3","q4"
    )

    flash_demo[ ,question_names] <- do.call(
      data.frame,
      lapply(
        flash_demo[ ,question_names],
        function(x) {
          factor(
            as.character(x),
            levels = c("0", "1"),
            labels = c("No", "Yes")
          )
        }
      )
    )

    names(flash_demo)[names(flash_demo) %in% question_names] <- c(
      "currently_active", "intend_more_active_6mo",
      "currently_engage_regular_pa", "regularly_active_last_6mo"
    )

  ## Health Perceptions

    perceptions <- c(
      "Poor", "Fair", "Good", "Very good", "Excellent"
    )

    flash_demo$perceptions_of_health <- sapply(
      flash_demo$perceptions_of_health,
      function (x) {
        if (is.na(x)) {
          return(NA)
        } else { return(
            switch(
              x, "A" = "Poor", "B" = "Fair", "C" = "Good",
              "D" = "Very_Good", "E" = "Excellent"
            )
          )
        }
      }
    )

    flash_demo$perceptions_of_health <- factor(
      flash_demo$perceptions_of_health, levels = perceptions
    )

  ## Handedness

    flash_demo$handedness %<>% factor(c("R", "L"))

  ## Weight status

    flash_demo$weight_status %<>% factor(
      levels = c(
        "Underweight", "Normal Weight", "Overweight",
        "Obese_1", "Obese_2", "Obese_3"
      )
    )

# Save Data ---------------------------------------------------------------

  saveRDS(
    flash_demo,
    "data-raw/Demographic/sample_code/sample_output.rds"
  )

  ## Read back in
  #
  # flash_demo <- readRDS(
  #   "data-raw/Demographic/sample_code/sample_output.rds"
  # )
PAHPLabResearch/FLASH documentation built on May 15, 2020, 7:08 p.m.