Create a pivot table.
pivot_table(
data = NULL,
row_names = NULL,
col_names = NULL,
function_as_character = NULL,
sigfigs = 3,
output = "dt",
remove_col_names = TRUE
)
the output will be a contingency table in a data.table format
a data object (a data frame or a data.table)
names of variables for constructing rows
names of variables for constructing columns independent variables
function to perform for each cell in the pivot table
number of significant digits to which to round values in the pivot table (default = 3)
type of output. If output = "dt"
, the function's
output will be a pivot table in a data.table format.
If output = "subsets"
, the function's output will be a list of
data tables that are subsets representing each cell in the pivot table.
By default, output = "dt"
logical. Should the column names (i.e., v1, v2, ...) be removed in the data table output?
pivot_table(
data = mtcars, col_names = "am", row_names = c("cyl", "vs"),
function_as_character = "mean(mpg)")
pivot_table(
data = mtcars, col_names = "am", row_names = c("cyl", "vs"),
function_as_character = "sum(mpg < 17)")
pivot_table(
data = mtcars, col_names = "am", row_names = c("cyl", "vs"),
function_as_character =
"round(sum(mpg < 17) / sum(!is.na(mpg)) * 100, 0)")
Run the code above in your browser using DataLab