This function converts dummy-coded CBC designs or choice data back to their original categorical format. This is useful for displaying choice questions to respondents or for analysis that requires categorical variables. Only works for data without no-choice options, as no-choice data cannot be meaningfully converted back to categorical format.
Examples
library(cbcTools)
# Create profiles with categorical variables
profiles <- cbc_profiles(
price = c(10, 20, 30),
quality = c("Low", "Medium", "High"),
brand = c("A", "B")
)
# Create design (will be dummy-coded by default)
design <- cbc_design(
profiles = profiles,
n_alts = 2,
n_q = 4
)
# Convert design back to categorical format
design_categorical <- cbc_decode(design)
head(design_categorical)
#> Design method: random
#> Structure: 100 respondents × 4 questions × 2 alternatives
#> Profile usage: 18/18 (100.0%)
#>
#> 💡 Use cbc_inspect() for a more detailed summary
#>
#> First few rows of design:
#> profileID respID qID altID obsID price quality brand
#> 1 7 1 1 1 1 10 High A
#> 2 11 1 1 2 1 20 Low B
#> 3 11 1 2 1 2 20 Low B
#> 4 1 1 2 2 2 10 Low A
#> 5 10 1 3 1 3 10 Low B
#> 6 5 1 3 2 3 20 Medium A
# Also works with choice data
choices <- cbc_choices(design)
choices_categorical <- cbc_decode(choices)
head(choices_categorical)
#> CBC Choice Data
#> ===============
#> Observations: 3 choice tasks
#> Alternatives per task: 2
#> Total choices made: 3
#>
#> Simulation method: random
#> Priors: None (random choices)
#> Simulated at: 2025-07-08 13:36:54
#>
#> Choice rates by alternative:
#> Alt 1: 66.7% (2 choices)
#> Alt 2: 33.3% (1 choices)
#>
#> First few rows:
#> profileID respID qID altID obsID price choice quality brand
#> 1 7 1 1 1 1 10 0 High A
#> 2 11 1 1 2 1 20 1 Low B
#> 3 11 1 2 1 2 20 1 Low B
#> 4 1 1 2 2 2 10 0 Low A
#> 5 10 1 3 1 3 10 1 Low B
#> 6 5 1 3 2 3 20 0 Medium A