xtab2df: Extract survey data and descriptions from crosstabs into a...

View source: R/xtab2df.R

xtab2dfR Documentation

Extract survey data and descriptions from crosstabs into a tidy data frame

Description

Like read_xtab & read_weights, this is a bespoke function to make it easier to extract data from the DataHaven Community Wellbeing Survey. Applications to other crosstabs are probably limited unless their formatting is largely the same. After reading a crosstab excel file, xtab2df extracts the question codes (e.g. Q4A), question text, categories, and demographic groups, and joins those descriptions with survey responses and values, making it ready for analysis.

Usage

xtab2df(.data, col = x1, code_pattern = "^[A-Z\\d_]{2,20}$")

Arguments

.data

A data frame as returned from read_xtab.

col

The bare column name of where to find question codes and text. Default: x1, based on names assigned by read_xtab

code_pattern

String: regex pattern denoting how to find cells that contain only a question code, such as "Q10", "Q4B", or "ASTHMA". This is pretty finicky, so you probably don't want to change it. Default: "^[A-Z\\d_]{2,20}$"

Value

A data frame with the following columns:

  • code (if questions have codes in crosstabs)

  • q_number (if questions don't have codes in crosstabs, assigned in order they occur)

  • question

  • category (e.g. age, gender)

  • group (e.g. 18–34, male)

  • response

  • value

See Also

read_xtabs()

Examples

if(interactive()){
  xtab <- read_xtabs(system.file("extdata/test_xtab2018.xlsx", package = "cwi"))
  xtab2df(xtab)
 }

CT-Data-Haven/cwi documentation built on July 1, 2024, 7:45 a.m.