pivot_table function

Pivot Table

Pivot Table

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 )

Arguments

  • data: a data object (a data frame or a data.table)
  • row_names: names of variables for constructing rows
  • col_names: names of variables for constructing columns independent variables
  • function_as_character: function to perform for each cell in the pivot table
  • sigfigs: number of significant digits to which to round values in the pivot table (default = 3)
  • output: 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"
  • remove_col_names: logical. Should the column names (i.e., v1, v2, ...) be removed in the data table output?

Returns

the output will be a contingency table in a data.table format

Examples

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)")