Skip to contents

Create an expss table object and a cleaned data frame from an input data frame.

Usage

make_banners(
  data,
  include.no.answer = TRUE,
  row.vars,
  col.vars,
  weight.var = NULL,
  date.var = NULL,
  preview = FALSE,
  total.row.position = "above",
  verbose = FALSE
)

Arguments

data

input data frame of survey responses.

include.no.answer

if TRUE, include "No answer" as a cell in cross-tabs, otherwise remove / treat as NA.

row.vars

list of column names in data to put in the banner rows; any multi-response variables can be specified either as a regex string (e.g. "var_*") or as a nested vector of all of the option columns (e.g. c("var_1", "var_2",...)).

col.vars

list of column names in data to put in the banner columns; any multi-response variables can be specified either as a regex string (e.g. "var_*") or as a nested vector of all of the option columns (e.g. c("var_1", "var_2",...)).

weight.var

optional column name in data to use for weighting.

date.var

optional column name in data to specify for weighting.

preview

if true, print the expss banner table object in a pretty format.

total.row.position

where to place the total or "Unweighted N"s (either "above" or "below", default is "above").

verbose

output helpful debugging messages.

Value

a list of banner outputs including the expss object and a cleaned data frame.

Details

Before running, must make sure that the input data is properly labelled/coded according to expss conventions (see example below). When doing so, make sure that values across multi-response variables (i.e. 'select all that apply' type questions) are given distinct levels, that is converted from binary to categorical (see example).

Examples

data(ev22)
if (FALSE) { ## not run
# Prepare data for banners
ev22.coded = ev22 %>%
  let(
    ## code/label categorical variables
    age3 = recode(age,
                  "18-34" = 18 %thru% 34 ~ 1, 
                  "35-64" = 35 %thru% 64 ~ 2, 
                  "65+"   = 65 %thru% hi ~ 3),
    gender  = recode(gender,
                     "Male"   = 1 ~ 1,
                     "Female" = 2 ~ 2),
    income3 = recode(as.numeric(income),
                     "<$50k"     = 1 %thru% 3 ~ 1,
                     "$50k-100k" = 4 %thru% 5 ~ 2,
                     ">$100k"    = 6 %thru% 7 ~ 3,
                     "No answer" = 8 ~ 4),
    educ2 = recode(as.numeric(education),
                   "<HS-College" = 1 %thru% 4 ~ 1,
                   ">College"    = 5 %thru% 6 ~ 2),
    purchase = recode(purchase,
                      "Purchased new"                            = 1 ~ 1,
                      "Purchased used (non-certified pre-owned)" = 2 ~ 2,
                      "Purchased used (certified pre-owned)"     = 3 ~ 3,
                      "Leased new"                               = 4 ~ 4,
                      "Leased used"                              = 5 ~ 5,
                      "Other"                                    = 6 ~ 6),
    vehage = recode(vehage,
                    "Less than 1 year old"   = 1 ~ 1,
                    "1 - 2 years old"        = 2 ~ 2,
                    "3 - 5 years old"        = 3 ~ 3,
                    "6 - 10 years old"       = 4 ~ 4,
                    "More than 10 years old" = 5 ~ 5),
    vehcost = recode(vehcost,
                     "Under $5,000"      = 1 ~ 1,
                     "$5,000 - $9,999"   = 2 ~ 2,
                     "$10,000 - $19,999" = 3 ~ 3,
                     "$20,000 - $29,999" = 4 ~ 4,
                     "$30,000 - $39,999" = 5 ~ 5,
                     "$40,000 - $49,999" = 6 ~ 6,
                     "$50,000 - $59,999" = 7 ~ 7,
                     "$60,000 or more"   = 8 ~ 8),
    ## label multi-response variables
    ## note: make sure to re-code dichotomous "one-hot encoded" columns into distinct level
    ev_heard_1 = recode(ev_heard_1, "Audi e-tron" = 1 ~ 1),
    ev_heard_2 = recode(ev_heard_2, "BMW i3" = 1 ~ 2),
    ev_heard_3 = recode(ev_heard_3, "Cadillac LYRIQ" = 1 ~ 3),
    ev_heard_4 = recode(ev_heard_4, "Chevrolet Bolt" = 1 ~ 4),
    ev_heard_5 = recode(ev_heard_5, "Faraday Future" = 1 ~ 5),
    ev_heard_6 = recode(ev_heard_6, "Fisker" = 1 ~ 6),
    ev_heard_7 = recode(ev_heard_7, "Hyundai Electric" = 1 ~ 7),
    ev_heard_8 = recode(ev_heard_8, "Jaguar I-Pace" = 1 ~ 8),
    ev_heard_9 = recode(ev_heard_9, "Kia Niro EV" = 1 ~ 9),
    ev_heard_10 = recode(ev_heard_10, "Lucid Motors" = 1 ~ 10),
    
    mech_factors_1 = recode(mech_factors_1, "Price / affordability" = 1 ~ 1),
    mech_factors_2 = recode(mech_factors_2, "Convenience" = 1 ~ 2),
    mech_factors_3 = recode(mech_factors_3, "Trust / relationship" = 1 ~ 3),
    mech_factors_4 = recode(mech_factors_4, "Availability" = 1 ~ 4),
    
    sub_services_1 = recode(sub_services_1, "In-car internet" = 1 ~ 1),
    sub_services_2 = recode(sub_services_1, "Remote car start capability" = 1 ~ 2),
    sub_services_3 = recode(sub_services_1, "Live traffic / navigation" = 1 ~ 3),
    sub_services_4 = recode(sub_services_1, "In-vehicle safety and security" = 1 ~ 4),
    sub_services_5 = recode(sub_services_1, "Multiple driver profiles" = 1 ~ 5),
    sub_services_6 = recode(sub_services_1, "Cloud dash cam monitoring" = 1 ~ 6),
    sub_services_7 = recode(sub_services_1, "Car software updates" = 1 ~ 7),
    sub_services_8 = recode(sub_services_1, "AI- and self-driving capabilities" = 1 ~ 8),
    sub_services_9 = recode(sub_services_1, "Car-only media and streaming services" = 1 ~ 9),
    sub_services_10 = recode(sub_services_1, "Heated seats" = 1 ~ 10)
  ) %>%
  apply_labels(gender         = "Gender",
               age3           = "Age: 3 categories",
               educ2          = "Education",
               income3        = "Income",
               vehage         = "How old is your main vehicle?",
               vehcost        = "In total, how much did you pay to buy or lease your main vehicle?",
               purchase       = "How did you purchase or lease your main vehicle?",
               ## note: for multiresponse variables, only need to name one column
               ev_heard_1     = "Which of the following electric vehicles have you heard of?",
               mech_factors_1 = "Select the top 3 factors that are most important to you when selecting an auto repair provider:",
               sub_services_1 = "Which of the following services would you be willing to pay a recurring subscription for in a car?") 
# Make and save banners  
ev22.banners <- ev22.coded %>%
  make_banners(row.vars           = list("purchase","vehage","ev_heard*",c("mech_factors_1","mech_factors_2","mech_factors_3","mech_factors_4")), 
               col.vars           = list("age3","educ2",c("sub_services_1","sub_services_2","sub_services_3","sub_services_4")), 
               weight.var         = "weight_genpop", 
               date.var           = "response_date", 
               total.row.position = "below", 
               preview            = TRUE) %>%
  write_banners(file.path         = "ev22.xlsx",
                file.overwrite    = TRUE,
                title             = "Electric Vehicles Study",
                logo              = "mntv",
                drive.overwrite   = TRUE,
                drive.folder.path = "https://drive.google.com/drive/u/1/folders/0B-OW6-tDrcdMTWw1MFFhdVNQLTg")
}