Takes a wide survey data frame (e.g., from read_Qualtrics) and reshapes
it so that each row corresponds to a single respondent–task–profile. Supports arbitrary
ordering of base tasks and a single repeated task per respondent. The repeated base task
is inferred from the first base outcome in .outcomes, and the repeated outcome
must be the last element of .outcomes.
reshape_projoint(
.dataframe,
.outcomes,
.choice_labels = c("A", "B"),
.alphabet = "K",
.idvar = "ResponseId",
.repeated = TRUE,
.flipped = TRUE,
.covariates = NULL,
.fill = FALSE
)A projoint_data object with elements $labels and $data; see Details.
A data frame, preferably from read_Qualtrics.
Character vector of outcome column names in the asked order. If a repeated task is used, its outcome must be the last element.
Character vector (default c("A","B")) giving the two labels that
appear at the end of the outcome strings.
Single character (default "K") indicating the Qualtrics prefix.
Character (default "ResponseId") indicating the respondent id column.
Logical (default TRUE) indicating whether a repeated task is present.
Logical (default TRUE) indicating whether the repeated task flips profiles
before agreement is computed.
Optional character vector of respondent-level covariate column names to carry through.
Logical (default FALSE). If TRUE, fills agree within respondent
across tasks as described under “Filling agreement”.
Scope and assumptions
One set of conjoint tasks with exactly two profiles per task (profiles 1 and 2).
For multi-set designs, call reshape_projoint() once per set and bind the results.
Expected input (Qualtrics K-codes)
Wide columns named K-<task>-<attribute> (attribute names) and
K-<task>-<profile>-<attribute> (level names), with <task> in 1..n
and <profile> in 1,2.
Rows with missing K-1-1 are dropped as empty tables (server hiccup safeguard).
Outcome columns (.outcomes)
List all choice variables in the order asked. If you include a repeated task, its outcome must be the last element.
For base tasks (all but the last element), the function extracts the base task id by
reading the digits in each outcome name (e.g., "choice4", "Q4",
"task04" -> task 4).
The set of base task ids extracted from .outcomes must exactly match the set of
task ids present in the K-codes; otherwise an error is thrown.
The repeated base task is inferred as the digits in the first base outcome
(i.e., the first element of .outcomes, excluding the final repeated outcome).
Choice parsing
The selected profile is parsed from the last character of each outcome string
and matched to .choice_labels. Ensure outcomes end with these labels (e.g.,
"Candidate A"/"Candidate B"). If outcomes are numeric or differently
formatted, pre-process or adjust .choice_labels accordingly.
Output
A projoint_data object with:
$labels: map from human-readable attribute/level to stable ids
(attribute_id = "att1","att2",..., level_id = "attX:levelY").
$data: tibble with one row per id–task–profile, attribute
columns (named att*) storing level_id, selected (1 if that profile
was chosen; 0 otherwise), agree (1/0/NA for repeated-task agreement after flip logic),
and any .covariates. id is coerced to character; attribute columns are factors.
Filling agreement
If .fill = TRUE, agree is filled within respondent across tasks in task order,
propagating the observed repeated-task agreement to all tasks for that respondent. This assumes
IRR is respondent-specific and independent of table content.
Diagnostics
dplyr::count(reshaped$data, task, profile) should show exactly two rows per task.
If pj_estimate() later reports “No rows match the specified attribute/level”, construct QoIs
from reshaped$labels (use the exact attX:levelY ids).
make_projoint_data, projoint
# \donttest{
# Base tasks asked in numeric order; repeated task corresponds to task 1
data(exampleData1)
outcomes <- c(paste0("choice", 1:8), "choice1_repeated_flipped")
reshaped <- reshape_projoint(exampleData1, outcomes)
dplyr::count(reshaped$data, task, profile) # should be 2 per task
# }
Run the code above in your browser using DataLab