```
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"
)
# Simple example - there is special shortcut for it - 'cross_mean'
cross_fun(mtcars,
list(mpg, disp, hp, wt, qsec),
col_vars = list(total(), am),
row_vars = vs,
fun = mean)
# The same example with 'subgroup'
cross_fun(mtcars,
list(mpg, disp, hp, wt, qsec),
col_vars = list(total(), am),
row_vars = vs,
subgroup = vs == 0,
fun = mean)
# 'combine_functions' usage
cross_fun(mtcars,
list(mpg, disp, hp, wt, qsec),
col_vars = list(total(), am),
row_vars = vs,
fun = combine_functions(Mean = mean,
'Std. dev.' = sd,
'Valid N' = valid_n)
)
# 'combine_functions' usage - statistic labels in columns
cross_fun(mtcars,
list(mpg, disp, hp, wt, qsec),
col_vars = list(total(), am),
row_vars = vs,
fun = combine_functions(Mean = mean,
'Std. dev.' = sd,
'Valid N' = valid_n,
method = list
)
)
# 'summary' function
cross_fun(mtcars,
list(mpg, disp, hp, wt, qsec),
col_vars = list(total(), am),
row_vars = list(total(), vs),
fun = summary
)
# comparison 'cross_fun' and 'cross_fun_df'
cross_fun(mtcars,
data.frame(mpg, disp, hp, wt, qsec),
col_vars = am,
fun = mean
)
# same result
cross_fun_df(mtcars,
data.frame(mpg, disp, hp, wt, qsec),
col_vars = am,
fun = colMeans
)
# usage for 'cross_fun_df' which is not possible for 'cross_fun'
# linear regression by groups
cross_fun_df(mtcars,
data.frame(mpg, disp, hp, wt, qsec),
col_vars = am,
fun = function(x){
frm = reformulate(".", response = as.name(names(x)[1]))
model = lm(frm, data = x)
cbind('Coef.' = coef(model),
confint(model)
)
}
)
```

Run the code above in your browser using DataCamp Workspace