Skip to contents

Formatters for pivot_format

Usage

pivot_formatter(
  n = round,
  p = function(x) {     paste0(round(x, 1), "%") },
  p_col = function(x) {     paste0(round(x, 1), "%") },
  p_row = function(x) {     paste0(round(x, 1), "%") }
)

Arguments

n

Function, applied to n.

p

Function, applied to p.

p_col

Function, applied to p_col.

p_row

Function, applied to p_row.

Value

a list of functions that can be use in pivot_format.

Examples

library(flexpivot)
data("nobel_laureates")

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

pivot_format(pt)

category

N

%

Chemistry

184

19.4%

Economic Sciences

84

8.8%

Literature

116

12.2%

Peace

134

14.1%

Physics

213

22.4%

Physiology or Medicine

219

23%

Total

950

100%

pivot_format(pt, formatter = pivot_formatter( n = function(x) { format(round(x * 100), big.mark = " ") } ))

category

N

%

Chemistry

18 400

19.4%

Economic Sciences

8 400

8.8%

Literature

11 600

12.2%

Peace

13 400

14.1%

Physics

21 300

22.4%

Physiology or Medicine

21 900

23%

Total

95 000

100%

# Two variable as rows pt <- pivot_table(nobel_laureates, rows = c("category", "gender")) pivot_format(pt)

category

gender

N

%

Chemistry

male

179

18.8%

female

5

0.5%

<missing>

0

0%

Total

184

19.4%

Economic Sciences

male

82

8.6%

female

2

0.2%

<missing>

0

0%

Total

84

8.8%

Literature

male

101

10.6%

female

15

1.6%

<missing>

0

0%

Total

116

12.2%

Peace

male

90

9.5%

female

17

1.8%

<missing>

27

2.8%

Total

134

14.1%

Physics

male

210

22.1%

female

3

0.3%

<missing>

0

0%

Total

213

22.4%

Physiology or Medicine

male

207

21.8%

female

12

1.3%

<missing>

0

0%

Total

219

23%

Total

male

869

91.5%

female

54

5.7%

<missing>

27

2.8%

Total

950

100%

pivot_format(pt, formatter = pivot_formatter( n = function(x) { format(round(x * 100), big.mark = " ") } ))

category

gender

N

%

Chemistry

male

17 900

18.8%

female

500

0.5%

<missing>

0

0%

Total

18 400

19.4%

Economic Sciences

male

8 200

8.6%

female

200

0.2%

<missing>

0

0%

Total

8 400

8.8%

Literature

male

10 100

10.6%

female

1 500

1.6%

<missing>

0

0%

Total

11 600

12.2%

Peace

male

9 000

9.5%

female

1 700

1.8%

<missing>

2 700

2.8%

Total

13 400

14.1%

Physics

male

21 000

22.1%

female

300

0.3%

<missing>

0

0%

Total

21 300

22.4%

Physiology or Medicine

male

20 700

21.8%

female

1 200

1.3%

<missing>

0

0%

Total

21 900

23%

Total

male

86 900

91.5%

female

5 400

5.7%

<missing>

2 700

2.8%

Total

95 000

100%

# One row, one column pt <- pivot_table(nobel_laureates, rows = "category", cols = "gender") pivot_format(pt)

gender

category

Statistic

male

female

<missing>

Total

Chemistry

N

179

5

0

184

%

18.8%

0.5%

0%

19.4%

Col %

20.6%

9.3%

0%

19.4%

Row %

97.3%

2.7%

0%

100%

Economic Sciences

N

82

2

0

84

%

8.6%

0.2%

0%

8.8%

Col %

9.4%

3.7%

0%

8.8%

Row %

97.6%

2.4%

0%

100%

Literature

N

101

15

0

116

%

10.6%

1.6%

0%

12.2%

Col %

11.6%

27.8%

0%

12.2%

Row %

87.1%

12.9%

0%

100%

Peace

N

90

17

27

134

%

9.5%

1.8%

2.8%

14.1%

Col %

10.4%

31.5%

100%

14.1%

Row %

67.2%

12.7%

20.1%

100%

Physics

N

210

3

0

213

%

22.1%

0.3%

0%

22.4%

Col %

24.2%

5.6%

0%

22.4%

Row %

98.6%

1.4%

0%

100%

Physiology or Medicine

N

207

12

0

219

%

21.8%

1.3%

0%

23%

Col %

23.8%

22.2%

0%

23%

Row %

94.5%

5.5%

0%

100%

Total

N

869

54

27

950

%

91.5%

5.7%

2.8%

100%

Col %

100%

100%

100%

100%

Row %

91.5%

5.7%

2.8%

100%

pivot_format(pt, formatter = pivot_formatter( n = function(x) { format(round(x * 100), big.mark = " ") } ))

gender

category

Statistic

male

female

<missing>

Total

Chemistry

N

17 900

500

0

18 400

%

18.8%

0.5%

0%

19.4%

Col %

20.6%

9.3%

0%

19.4%

Row %

97.3%

2.7%

0%

100%

Economic Sciences

N

8 200

200

0

8 400

%

8.6%

0.2%

0%

8.8%

Col %

9.4%

3.7%

0%

8.8%

Row %

97.6%

2.4%

0%

100%

Literature

N

10 100

1 500

0

11 600

%

10.6%

1.6%

0%

12.2%

Col %

11.6%

27.8%

0%

12.2%

Row %

87.1%

12.9%

0%

100%

Peace

N

9 000

1 700

2 700

13 400

%

9.5%

1.8%

2.8%

14.1%

Col %

10.4%

31.5%

100%

14.1%

Row %

67.2%

12.7%

20.1%

100%

Physics

N

21 000

300

0

21 300

%

22.1%

0.3%

0%

22.4%

Col %

24.2%

5.6%

0%

22.4%

Row %

98.6%

1.4%

0%

100%

Physiology or Medicine

N

20 700

1 200

0

21 900

%

21.8%

1.3%

0%

23%

Col %

23.8%

22.2%

0%

23%

Row %

94.5%

5.5%

0%

100%

Total

N

86 900

5 400

2 700

95 000

%

91.5%

5.7%

2.8%

100%

Col %

100%

100%

100%

100%

Row %

91.5%

5.7%

2.8%

100%