if (FALSE) {
data(mtcars)
mtcars = apply_labels(mtcars,
mpg = "Miles/(US) gallon",
cyl = "Number of cylinders",
disp = "Displacement (cu.in.)",
hp = "Gross horsepower",
drat = "Rear axle ratio",
wt = "Weight (1000 lbs)",
qsec = "1/4 mile time",
vs = "Engine",
vs = c("V-engine" = 0,
"Straight engine" = 1),
am = "Transmission",
am = c("Automatic" = 0,
"Manual"=1),
gear = "Number of forward gears",
carb = "Number of carburetors"
)
cross_cases(mtcars, am, vs)
cro(mtcars$am, mtcars$vs) # the same result
# column percent with multiple banners
cross_cpct(mtcars, cyl, list(total(), vs, am))
# nested banner
cross_cpct(mtcars, cyl, list(total(), vs %nest% am))
# stacked variables
cross_cases(mtcars, list(cyl, carb), list(total(), vs %nest% am))
# nested variables
cross_cpct(mtcars, am %nest% cyl, list(total(), vs))
# row variables
cross_cpct(mtcars, cyl, list(total(), vs), row_vars = am)
# several totals above table
cross_cpct(mtcars, cyl,
list(total(), vs),
row_vars = am,
total_row_position = "above",
total_label = c("number of cases", "row %"),
total_statistic = c("u_cases", "u_rpct")
)
# multiple-choice variable
# brands - multiple response question
# Which brands do you use during last three months?
set.seed(123)
brands = data.table(t(replicate(20,sample(c(1:5,NA),4,replace = FALSE)))) %>%
setNames(paste0("brand_", 1:4))
# score - evaluation of tested product
brands = brands %>%
let(
score = sample(-1:1,.N,replace = TRUE)
) %>%
apply_labels(
brand_1 = "Used brands",
brand_1 = num_lab("
1 Brand A
2 Brand B
3 Brand C
4 Brand D
5 Brand E
"),
score = "Evaluation of tested brand",
score = num_lab("
-1 Dislike it
0 So-so
1 Like it
")
)
cross_cpct(brands, mrset(brand_1 %to% brand_4), list(total(), score))
# responses
cross_cpct_responses(brands, mrset(brand_1 %to% brand_4), list(total(), score))
}
Run the code above in your browser using DataLab