Skip to contents

Attaches persona metadata to a decoded data frame. When a persona_frame is passed to panel_from_data() without a template, demographic fields and stated answers are rendered separately. A question map supplies the wording used for answer fields. Plain data frames use the flat key-value rendering.

Usage

as_persona_frame(data, questions = NULL, demographics = NULL, answers = NULL)

Arguments

data

A decoded data frame, one respondent per row. Values should already be human-readable labels (decode a labelled survey file with, for example, haven::as_factor() first).

questions

Optional named character vector mapping column names to the human question wording, e.g. c(pid = "Party identification", ab = "Abortion position"). Columns absent from this map keep their column name. Without it the column names stand in for the questions, which is formatting, not a faithful translation.

demographics

Optional character vector of columns to treat as demographic background (the rest become stated answers). Defaults to the common demographic names found in data.

answers

Optional character vector restricting which columns may appear as stated answers. Defaults to every column that is not a demographic, an id-named, or a weight-named column, so analysis-only columns do not leak into the prompt.

Value

data with the persona contract attached and class persona_frame.

Examples

df <- data.frame(
  age = c("35-44", "65+"),
  pid = c("Strong Democrat", "Strong Republican"),
  ab  = c("Always legal", "Never legal"))
pf <- as_persona_frame(
  df,
  questions = c(pid = "Party identification", ab = "Abortion position"),
  demographics = "age")