pivot_table: Create a pivot table

View source: R/pivot_table.R

pivot_tableR Documentation

Create a pivot table

Description

Create a pivot table

Usage

pivot_table(
  data,
  rows,
  cols = NULL,
  wt = NULL,
  stats = c("n", "p", "p_row", "p_col"),
  digits = 2,
  total = TRUE,
  total_label = "Total",
  na_label = "<missing>",
  complete = TRUE
)

Arguments

data

a data.frame.

rows

Character vector of variable(s) to use as rows.

cols

Character vector of variable(s) to use as columns.

wt

Character, variable to use as weights if any.

stats

Statistic(s) to compute.

digits

Integer indicating the number of decimal places to be used.

total

Logical, add total or not.

total_label

Label to use fo total.

na_label

Label to use for missing values.

complete

Complete missing combination between rows if several and cols if any.

Value

a data.table

Examples

library(flexpivot)
data("nobel_laureates")

# One variable
pivot_table(nobel_laureates, rows = "category")

# With two variables
pivot_table(nobel_laureates, rows = "category", cols = "birth_continent")

# Only count
pivot_table(nobel_laureates, rows = "category", cols = "gender", stats = "n")

# Only percent
pivot_table(nobel_laureates, rows = "category", cols = "gender", stats = "p")

# Without total
pivot_table(nobel_laureates, rows = "category", cols = "gender", total = FALSE)


# Two variable as rows
pivot_table(nobel_laureates, rows = c("birth_continent", "category"), cols = "gender")

# Two variable as cols
pivot_table(nobel_laureates, rows = "category", cols = c("gender", "laureate_type"))


# Round
pivot_table(nobel_laureates, rows = "category", digits = 0)

# Without cols
pivot_table(nobel_laureates, rows = "category")
pivot_table(nobel_laureates, rows = c("category", "birth_continent"))

dreamRs/flexpivot documentation built on Oct. 26, 2023, 9:46 a.m.