Create a pivot table
pivot_table.Rd
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 andcols
if any.
Examples
library(flexpivot)
data("nobel_laureates")
# One variable
pivot_table(nobel_laureates, rows = "category")
#> category n p
#> 1: Chemistry 184 19.37
#> 2: Economic Sciences 84 8.84
#> 3: Literature 116 12.21
#> 4: Peace 134 14.11
#> 5: Physics 213 22.42
#> 6: Physiology or Medicine 219 23.05
#> 7: Total 950 100.00
# With two variables
pivot_table(nobel_laureates, rows = "category", cols = "birth_continent")
#> category stats Europe Oceania North America Africa Asia
#> 1: Chemistry n 103.00 3.00 59.00 3.00 16.00
#> 2: Chemistry p 10.84 0.32 6.21 0.32 1.68
#> 3: Chemistry p_col 20.85 20.00 19.41 11.11 22.22
#> 4: Chemistry p_row 55.98 1.63 32.07 1.63 8.70
#> 5: Economic Sciences n 30.00 0.00 51.00 0.00 3.00
#> 6: Economic Sciences p 3.16 0.00 5.37 0.00 0.32
#> 7: Economic Sciences p_col 6.07 0.00 16.78 0.00 4.17
#> 8: Economic Sciences p_row 35.71 0.00 60.71 0.00 3.57
#> 9: Literature n 80.00 0.00 16.00 6.00 10.00
#> 10: Literature p 8.42 0.00 1.68 0.63 1.05
#> 11: Literature p_col 16.19 0.00 5.26 22.22 13.89
#> 12: Literature p_row 68.97 0.00 13.79 5.17 8.62
#> 13: Peace n 53.00 2.00 23.00 13.00 13.00
#> 14: Peace p 5.58 0.21 2.42 1.37 1.37
#> 15: Peace p_col 10.73 13.33 7.57 48.15 18.06
#> 16: Peace p_row 39.55 1.49 17.16 9.70 9.70
#> 17: Physics n 114.00 2.00 75.00 2.00 20.00
#> 18: Physics p 12.00 0.21 7.89 0.21 2.11
#> 19: Physics p_col 23.08 13.33 24.67 7.41 27.78
#> 20: Physics p_row 53.52 0.94 35.21 0.94 9.39
#> 21: Physiology or Medicine n 114.00 8.00 80.00 3.00 10.00
#> 22: Physiology or Medicine p 12.00 0.84 8.42 0.32 1.05
#> 23: Physiology or Medicine p_col 23.08 53.33 26.32 11.11 13.89
#> 24: Physiology or Medicine p_row 52.05 3.65 36.53 1.37 4.57
#> 25: Total n 494.00 15.00 304.00 27.00 72.00
#> 26: Total p 52.00 1.58 32.00 2.84 7.58
#> 27: Total p_col 100.00 100.00 100.00 100.00 100.00
#> 28: Total p_row 52.00 1.58 32.00 2.84 7.58
#> category stats Europe Oceania North America Africa Asia
#> South America <missing> Total
#> 1: 0.00 0.00 184.00
#> 2: 0.00 0.00 19.37
#> 3: 0.00 0.00 19.37
#> 4: 0.00 0.00 100.00
#> 5: 0.00 0.00 84.00
#> 6: 0.00 0.00 8.84
#> 7: 0.00 0.00 8.84
#> 8: 0.00 0.00 100.00
#> 9: 4.00 0.00 116.00
#> 10: 0.42 0.00 12.21
#> 11: 36.36 0.00 12.21
#> 12: 3.45 0.00 100.00
#> 13: 3.00 27.00 134.00
#> 14: 0.32 2.84 14.11
#> 15: 27.27 100.00 14.11
#> 16: 2.24 20.15 100.00
#> 17: 0.00 0.00 213.00
#> 18: 0.00 0.00 22.42
#> 19: 0.00 0.00 22.42
#> 20: 0.00 0.00 100.00
#> 21: 4.00 0.00 219.00
#> 22: 0.42 0.00 23.05
#> 23: 36.36 0.00 23.05
#> 24: 1.83 0.00 100.00
#> 25: 11.00 27.00 950.00
#> 26: 1.16 2.84 100.00
#> 27: 100.00 100.00 100.00
#> 28: 1.16 2.84 100.00
#> South America <missing> Total
# Only count
pivot_table(nobel_laureates, rows = "category", cols = "gender", stats = "n")
#> category stats male female <missing> Total
#> 1: Chemistry n 179 5 0 184
#> 2: Economic Sciences n 82 2 0 84
#> 3: Literature n 101 15 0 116
#> 4: Peace n 90 17 27 134
#> 5: Physics n 210 3 0 213
#> 6: Physiology or Medicine n 207 12 0 219
#> 7: Total n 869 54 27 950
# Only percent
pivot_table(nobel_laureates, rows = "category", cols = "gender", stats = "p")
#> category stats male female <missing> Total
#> 1: Chemistry p 18.84 0.53 0.00 19.37
#> 2: Economic Sciences p 8.63 0.21 0.00 8.84
#> 3: Literature p 10.63 1.58 0.00 12.21
#> 4: Peace p 9.47 1.79 2.84 14.11
#> 5: Physics p 22.11 0.32 0.00 22.42
#> 6: Physiology or Medicine p 21.79 1.26 0.00 23.05
#> 7: Total p 91.47 5.68 2.84 100.00
# Without total
pivot_table(nobel_laureates, rows = "category", cols = "gender", total = FALSE)
#> category stats male female <missing>
#> 1: Chemistry n 179.00 5.00 0.00
#> 2: Chemistry p 18.84 0.53 0.00
#> 3: Chemistry p_col 20.60 9.26 0.00
#> 4: Chemistry p_row 97.28 2.72 0.00
#> 5: Economic Sciences n 82.00 2.00 0.00
#> 6: Economic Sciences p 8.63 0.21 0.00
#> 7: Economic Sciences p_col 9.44 3.70 0.00
#> 8: Economic Sciences p_row 97.62 2.38 0.00
#> 9: Literature n 101.00 15.00 0.00
#> 10: Literature p 10.63 1.58 0.00
#> 11: Literature p_col 11.62 27.78 0.00
#> 12: Literature p_row 87.07 12.93 0.00
#> 13: Peace n 90.00 17.00 27.00
#> 14: Peace p 9.47 1.79 2.84
#> 15: Peace p_col 10.36 31.48 100.00
#> 16: Peace p_row 67.16 12.69 20.15
#> 17: Physics n 210.00 3.00 0.00
#> 18: Physics p 22.11 0.32 0.00
#> 19: Physics p_col 24.17 5.56 0.00
#> 20: Physics p_row 98.59 1.41 0.00
#> 21: Physiology or Medicine n 207.00 12.00 0.00
#> 22: Physiology or Medicine p 21.79 1.26 0.00
#> 23: Physiology or Medicine p_col 23.82 22.22 0.00
#> 24: Physiology or Medicine p_row 94.52 5.48 0.00
#> category stats male female <missing>
# Two variable as rows
pivot_table(nobel_laureates, rows = c("birth_continent", "category"), cols = "gender")
#> birth_continent category stats male female <missing>
#> 1: Europe Chemistry n 101.00 2.00 0.00
#> 2: Europe Chemistry p 10.63 0.21 0.00
#> 3: Europe Chemistry p_col 11.62 3.70 0.00
#> 4: Europe Chemistry p_row 98.06 1.94 0.00
#> 5: Europe Economic Sciences n 29.00 1.00 0.00
#> ---
#> 220: Total Physiology or Medicine p_row 94.52 5.48 0.00
#> 221: Total Total n 869.00 54.00 27.00
#> 222: Total Total p 91.47 5.68 2.84
#> 223: Total Total p_col 100.00 100.00 100.00
#> 224: Total Total p_row 91.47 5.68 2.84
#> Total
#> 1: 103.00
#> 2: 10.84
#> 3: 10.84
#> 4: 100.00
#> 5: 30.00
#> ---
#> 220: 100.00
#> 221: 950.00
#> 222: 100.00
#> 223: 100.00
#> 224: 100.00
# Two variable as cols
pivot_table(nobel_laureates, rows = "category", cols = c("gender", "laureate_type"))
#> category stats male_|_Individual male_|_Organisation
#> 1: Chemistry n 179.00 0
#> 2: Chemistry p 18.84 0
#> 3: Chemistry p_col 20.60 0
#> 4: Chemistry p_row 97.28 0
#> 5: Economic Sciences n 82.00 0
#> 6: Economic Sciences p 8.63 0
#> 7: Economic Sciences p_col 9.44 0
#> 8: Economic Sciences p_row 97.62 0
#> 9: Literature n 101.00 0
#> 10: Literature p 10.63 0
#> 11: Literature p_col 11.62 0
#> 12: Literature p_row 87.07 0
#> 13: Peace n 90.00 0
#> 14: Peace p 9.47 0
#> 15: Peace p_col 10.36 0
#> 16: Peace p_row 67.16 0
#> 17: Physics n 210.00 0
#> 18: Physics p 22.11 0
#> 19: Physics p_col 24.17 0
#> 20: Physics p_row 98.59 0
#> 21: Physiology or Medicine n 207.00 0
#> 22: Physiology or Medicine p 21.79 0
#> 23: Physiology or Medicine p_col 23.82 0
#> 24: Physiology or Medicine p_row 94.52 0
#> 25: Total n 869.00 0
#> 26: Total p 91.47 0
#> 27: Total p_col 100.00 0
#> 28: Total p_row 91.47 0
#> category stats male_|_Individual male_|_Organisation
#> male_|_Total female_|_Individual female_|_Organisation female_|_Total
#> 1: 179.00 5.00 0 5.00
#> 2: 18.84 0.53 0 0.53
#> 3: 20.60 9.26 0 9.26
#> 4: 97.28 2.72 0 2.72
#> 5: 82.00 2.00 0 2.00
#> 6: 8.63 0.21 0 0.21
#> 7: 9.44 3.70 0 3.70
#> 8: 97.62 2.38 0 2.38
#> 9: 101.00 15.00 0 15.00
#> 10: 10.63 1.58 0 1.58
#> 11: 11.62 27.78 0 27.78
#> 12: 87.07 12.93 0 12.93
#> 13: 90.00 17.00 0 17.00
#> 14: 9.47 1.79 0 1.79
#> 15: 10.36 31.48 0 31.48
#> 16: 67.16 12.69 0 12.69
#> 17: 210.00 3.00 0 3.00
#> 18: 22.11 0.32 0 0.32
#> 19: 24.17 5.56 0 5.56
#> 20: 98.59 1.41 0 1.41
#> 21: 207.00 12.00 0 12.00
#> 22: 21.79 1.26 0 1.26
#> 23: 23.82 22.22 0 22.22
#> 24: 94.52 5.48 0 5.48
#> 25: 869.00 54.00 0 54.00
#> 26: 91.47 5.68 0 5.68
#> 27: 100.00 100.00 0 100.00
#> 28: 91.47 5.68 0 5.68
#> male_|_Total female_|_Individual female_|_Organisation female_|_Total
#> <missing>_|_Individual <missing>_|_Organisation <missing>_|_Total
#> 1: 0 0.00 0.00
#> 2: 0 0.00 0.00
#> 3: 0 0.00 0.00
#> 4: 0 0.00 0.00
#> 5: 0 0.00 0.00
#> 6: 0 0.00 0.00
#> 7: 0 0.00 0.00
#> 8: 0 0.00 0.00
#> 9: 0 0.00 0.00
#> 10: 0 0.00 0.00
#> 11: 0 0.00 0.00
#> 12: 0 0.00 0.00
#> 13: 0 27.00 27.00
#> 14: 0 2.84 2.84
#> 15: 0 100.00 100.00
#> 16: 0 20.15 20.15
#> 17: 0 0.00 0.00
#> 18: 0 0.00 0.00
#> 19: 0 0.00 0.00
#> 20: 0 0.00 0.00
#> 21: 0 0.00 0.00
#> 22: 0 0.00 0.00
#> 23: 0 0.00 0.00
#> 24: 0 0.00 0.00
#> 25: 0 27.00 27.00
#> 26: 0 2.84 2.84
#> 27: 0 100.00 100.00
#> 28: 0 2.84 2.84
#> <missing>_|_Individual <missing>_|_Organisation <missing>_|_Total
#> Total_|_Individual Total_|_Organisation Total_|_Total
#> 1: 184.00 0.00 184.00
#> 2: 19.37 0.00 19.37
#> 3: 19.93 0.00 19.37
#> 4: 100.00 0.00 100.00
#> 5: 84.00 0.00 84.00
#> 6: 8.84 0.00 8.84
#> 7: 9.10 0.00 8.84
#> 8: 100.00 0.00 100.00
#> 9: 116.00 0.00 116.00
#> 10: 12.21 0.00 12.21
#> 11: 12.57 0.00 12.21
#> 12: 100.00 0.00 100.00
#> 13: 107.00 27.00 134.00
#> 14: 11.26 2.84 14.11
#> 15: 11.59 100.00 14.11
#> 16: 79.85 20.15 100.00
#> 17: 213.00 0.00 213.00
#> 18: 22.42 0.00 22.42
#> 19: 23.08 0.00 22.42
#> 20: 100.00 0.00 100.00
#> 21: 219.00 0.00 219.00
#> 22: 23.05 0.00 23.05
#> 23: 23.73 0.00 23.05
#> 24: 100.00 0.00 100.00
#> 25: 923.00 27.00 950.00
#> 26: 97.16 2.84 100.00
#> 27: 100.00 100.00 100.00
#> 28: 97.16 2.84 100.00
#> Total_|_Individual Total_|_Organisation Total_|_Total
# Round
pivot_table(nobel_laureates, rows = "category", digits = 0)
#> category n p
#> 1: Chemistry 184 19
#> 2: Economic Sciences 84 9
#> 3: Literature 116 12
#> 4: Peace 134 14
#> 5: Physics 213 22
#> 6: Physiology or Medicine 219 23
#> 7: Total 950 100
# Without cols
pivot_table(nobel_laureates, rows = "category")
#> category n p
#> 1: Chemistry 184 19.37
#> 2: Economic Sciences 84 8.84
#> 3: Literature 116 12.21
#> 4: Peace 134 14.11
#> 5: Physics 213 22.42
#> 6: Physiology or Medicine 219 23.05
#> 7: Total 950 100.00
pivot_table(nobel_laureates, rows = c("category", "birth_continent"))
#> category birth_continent n p
#> 1: Chemistry Europe 103 10.84
#> 2: Chemistry Oceania 3 0.32
#> 3: Chemistry North America 59 6.21
#> 4: Chemistry Africa 3 0.32
#> 5: Chemistry Asia 16 1.68
#> 6: Chemistry South America 0 0.00
#> 7: Chemistry <missing> 0 0.00
#> 8: Chemistry Total 184 19.37
#> 9: Economic Sciences Europe 30 3.16
#> 10: Economic Sciences Oceania 0 0.00
#> 11: Economic Sciences North America 51 5.37
#> 12: Economic Sciences Africa 0 0.00
#> 13: Economic Sciences Asia 3 0.32
#> 14: Economic Sciences South America 0 0.00
#> 15: Economic Sciences <missing> 0 0.00
#> 16: Economic Sciences Total 84 8.84
#> 17: Literature Europe 80 8.42
#> 18: Literature Oceania 0 0.00
#> 19: Literature North America 16 1.68
#> 20: Literature Africa 6 0.63
#> 21: Literature Asia 10 1.05
#> 22: Literature South America 4 0.42
#> 23: Literature <missing> 0 0.00
#> 24: Literature Total 116 12.21
#> 25: Peace Europe 53 5.58
#> 26: Peace Oceania 2 0.21
#> 27: Peace North America 23 2.42
#> 28: Peace Africa 13 1.37
#> 29: Peace Asia 13 1.37
#> 30: Peace South America 3 0.32
#> 31: Peace <missing> 27 2.84
#> 32: Peace Total 134 14.11
#> 33: Physics Europe 114 12.00
#> 34: Physics Oceania 2 0.21
#> 35: Physics North America 75 7.89
#> 36: Physics Africa 2 0.21
#> 37: Physics Asia 20 2.11
#> 38: Physics South America 0 0.00
#> 39: Physics <missing> 0 0.00
#> 40: Physics Total 213 22.42
#> 41: Physiology or Medicine Europe 114 12.00
#> 42: Physiology or Medicine Oceania 8 0.84
#> 43: Physiology or Medicine North America 80 8.42
#> 44: Physiology or Medicine Africa 3 0.32
#> 45: Physiology or Medicine Asia 10 1.05
#> 46: Physiology or Medicine South America 4 0.42
#> 47: Physiology or Medicine <missing> 0 0.00
#> 48: Physiology or Medicine Total 219 23.05
#> 49: Total Europe 494 52.00
#> 50: Total Oceania 15 1.58
#> 51: Total North America 304 32.00
#> 52: Total Africa 27 2.84
#> 53: Total Asia 72 7.58
#> 54: Total South America 11 1.16
#> 55: Total <missing> 27 2.84
#> 56: Total Total 950 100.00
#> category birth_continent n p