tab()
- Tabulates variable(s) simultaneously, similar tocount()
.tab1()
- Tabulates variable(s) one by one.tab2()
- Cross-tabulates two variables.fre()
- Adds “Total” row(s) to the output oftab()
.fre1()
- Adds “Total” row(s) to the output oftab1()
.fre2()
- Adds a “Total” row and a “total” column to the output oftab2()
.
tab()
functions are suitable for subsequent data analysis, while fre()
functions return flextables by default and are suitable for reporting.
If the output tibble has many rows, consider using
print()
or print_headtail()
to display the output.
Usage
tab(.data, ..., .by = NULL, .desc = FALSE)
tab1(.data, ..., .by = NULL, .desc = FALSE, .type = c("list", "tibble"))
tab2(.data, ..., .flip = FALSE)
fre(.data, ..., .by = NULL, .desc = FALSE, .type = c("flextable", "tibble"))
fre1(
.data,
...,
.by = NULL,
.desc = FALSE,
.type = c("flextable", "list", "tibble")
)
fre2(.data, ..., .flip = FALSE, .type = c("flextable", "tibble"))
Arguments
- .data
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).
- ...
<
tidy-select
> or <data-masking
> Variables to tabulate.Both tidyselect (e.g.,
starts_with()
) and data masking (e.g.,x_sq = x^2
) are supported. See examples below.- .by
-
<
tidy-select
> Optionally, a selection of columns to group by for just this operation, functioning as an alternative togroup_by()
. For details and examples, see ?dplyr_by. - .desc
If TRUE, will show the largest groups at the top.
- .type
Output type. tab1() returns a list by default, and fre() functions return flextables by default.
- .flip
If TRUE, will transpose the output tibble.
Examples
# tabulate the 8th column
tab(starwars, 8)
#> # A tibble: 5 × 6
#> sex n percent cum valid valid_cum
#> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 female 16 18.4 18.4 19.3 19.3
#> 2 hermaphroditic 1 1.15 19.5 1.20 20.5
#> 3 male 60 69.0 88.5 72.3 92.8
#> 4 none 6 6.90 95.4 7.23 100
#> 5 NA 4 4.60 100 NA NA
# tabulate the "sex" column
tab(starwars, sex)
#> # A tibble: 5 × 6
#> sex n percent cum valid valid_cum
#> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 female 16 18.4 18.4 19.3 19.3
#> 2 hermaphroditic 1 1.15 19.5 1.20 20.5
#> 3 male 60 69.0 88.5 72.3 92.8
#> 4 none 6 6.90 95.4 7.23 100
#> 5 NA 4 4.60 100 NA NA
tab(starwars, sex, .desc = TRUE)
#> # A tibble: 5 × 6
#> sex n percent cum valid valid_cum
#> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 male 60 69.0 69.0 72.3 72.3
#> 2 female 16 18.4 87.4 19.3 91.6
#> 3 none 6 6.90 94.3 7.23 98.8
#> 4 hermaphroditic 1 1.15 95.4 1.20 100
#> 5 NA 4 4.60 100 NA NA
fre(starwars, sex)
sex
n
percent
cum
valid
valid_cum
character
integer
numeric
numeric
numeric
numeric
female
16
18.4
18.4
19.3
19.3
hermaphroditic
1
1.1
19.5
1.2
20.5
male
60
69.0
88.5
72.3
92.8
none
6
6.9
95.4
7.2
100.0
Valid Total
83
95.4
100.0
NA
4
4.6
100.0
Total
87
100.0
n: 7
fre(starwars, sex, .desc = TRUE)
sex
n
percent
cum
valid
valid_cum
character
integer
numeric
numeric
numeric
numeric
male
60
69.0
69.0
72.3
72.3
female
16
18.4
87.4
19.3
91.6
none
6
6.9
94.3
7.2
98.8
hermaphroditic
1
1.1
95.4
1.2
100.0
Valid Total
83
95.4
100.0
NA
4
4.6
100.0
Total
87
100.0
n: 7
tab(starwars, sex, gender)
#> # A tibble: 6 × 7
#> sex gender n percent cum valid valid_cum
#> <chr> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 female feminine 16 18.4 18.4 19.3 19.3
#> 2 hermaphroditic masculine 1 1.15 19.5 1.20 20.5
#> 3 male masculine 60 69.0 88.5 72.3 92.8
#> 4 none feminine 1 1.15 89.7 1.20 94.0
#> 5 none masculine 5 5.75 95.4 6.02 100
#> 6 NA NA 4 4.60 100 NA NA
tab(starwars, sex == "male")
#> # A tibble: 3 × 6
#> `sex == "male"` n percent cum valid valid_cum
#> <lgl> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 FALSE 23 26.4 26.4 27.7 27.7
#> 2 TRUE 60 69.0 95.4 72.3 100
#> 3 NA 4 4.60 100 NA NA
tab(starwars, male = sex == "male")
#> # A tibble: 3 × 6
#> male n percent cum valid valid_cum
#> <lgl> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 FALSE 23 26.4 26.4 27.7 27.7
#> 2 TRUE 60 69.0 95.4 72.3 100
#> 3 NA 4 4.60 100 NA NA
tab(starwars, ends_with("color"))
#> # A tibble: 67 × 8
#> hair_color skin_color eye_color n percent cum valid valid_cum
#> <chr> <chr> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 auburn fair blue 1 1.15 1.15 1.22 1.22
#> 2 auburn, grey fair blue 1 1.15 2.30 1.22 2.44
#> 3 auburn, white fair blue-gray 1 1.15 3.45 1.22 3.66
#> 4 black blue, grey yellow 1 1.15 4.60 1.22 4.88
#> 5 black brown brown 1 1.15 5.75 1.22 6.10
#> 6 black dark brown 3 3.45 9.20 3.66 9.76
#> 7 black dark dark 1 1.15 10.3 1.22 11.0
#> 8 black fair brown 2 2.30 12.6 2.44 13.4
#> 9 black light brown 1 1.15 13.8 1.22 14.6
#> 10 black tan brown 2 2.30 16.1 2.44 17.1
#> # ℹ 57 more rows
tab(starwars, s_match("s*e"))
#> # A tibble: 1 × 5
#> n percent cum valid valid_cum
#> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 87 100 100 100 100
tab(starwars)
#> # A tibble: 87 × 19
#> name height mass hair_color skin_color eye_color birth_year sex gender
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Ackbar 180 83 none brown mot… orange 41 male mascu…
#> 2 Anakin … 188 84 blond fair blue 41.9 male mascu…
#> 3 Ayla Se… 178 55 none blue hazel 48 fema… femin…
#> 4 Barriss… 166 50 black yellow blue 40 fema… femin…
#> 5 Beru Wh… 165 75 brown light blue 47 fema… femin…
#> 6 Biggs D… 183 84 black light brown 24 male mascu…
#> 7 Boba Fe… 183 78.2 black fair brown 31.5 male mascu…
#> 8 Bossk 190 113 none green red 53 male mascu…
#> 9 Chewbac… 228 112 brown unknown blue 200 male mascu…
#> 10 Darth M… 175 80 none red yellow 54 male mascu…
#> # ℹ 77 more rows
#> # ℹ 10 more variables: homeworld <chr>, species <chr>, films <list>,
#> # vehicles <list>, starships <list>, n <int>, percent <dbl>, cum <dbl>,
#> # valid <dbl>, valid_cum <dbl>
tab1(starwars, 1:3)
#> $name
#> # A tibble: 87 × 6
#> value n percent cum valid valid_cum
#> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 Ackbar 1 1.15 1.15 1.15 1.15
#> 2 Adi Gallia 1 1.15 2.30 1.15 2.30
#> 3 Anakin Skywalker 1 1.15 3.45 1.15 3.45
#> 4 Arvel Crynyd 1 1.15 4.60 1.15 4.60
#> 5 Ayla Secura 1 1.15 5.75 1.15 5.75
#> 6 BB8 1 1.15 6.90 1.15 6.90
#> 7 Bail Prestor Organa 1 1.15 8.05 1.15 8.05
#> 8 Barriss Offee 1 1.15 9.20 1.15 9.20
#> 9 Ben Quadinaros 1 1.15 10.3 1.15 10.3
#> 10 Beru Whitesun lars 1 1.15 11.5 1.15 11.5
#> # ℹ 77 more rows
#>
#> $height
#> # A tibble: 46 × 6
#> value n percent cum valid valid_cum
#> <int> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 66 1 1.15 1.15 1.23 1.23
#> 2 79 1 1.15 2.30 1.23 2.47
#> 3 88 1 1.15 3.45 1.23 3.70
#> 4 94 1 1.15 4.60 1.23 4.94
#> 5 96 2 2.30 6.90 2.47 7.41
#> 6 97 1 1.15 8.05 1.23 8.64
#> 7 112 1 1.15 9.20 1.23 9.88
#> 8 122 1 1.15 10.3 1.23 11.1
#> 9 137 1 1.15 11.5 1.23 12.3
#> 10 150 2 2.30 13.8 2.47 14.8
#> # ℹ 36 more rows
#>
#> $mass
#> # A tibble: 39 × 6
#> value n percent cum valid valid_cum
#> <dbl> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 15 1 1.15 1.15 1.69 1.69
#> 2 17 1 1.15 2.30 1.69 3.39
#> 3 20 1 1.15 3.45 1.69 5.08
#> 4 32 2 2.30 5.75 3.39 8.47
#> 5 40 1 1.15 6.90 1.69 10.2
#> 6 45 2 2.30 9.20 3.39 13.6
#> 7 48 2 2.30 11.5 3.39 16.9
#> 8 49 1 1.15 12.6 1.69 18.6
#> 9 50 2 2.30 14.9 3.39 22.0
#> 10 55 2 2.30 17.2 3.39 25.4
#> # ℹ 29 more rows
#>
tab1(starwars, ends_with("color"))
#> $hair_color
#> # A tibble: 13 × 6
#> value n percent cum valid valid_cum
#> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 auburn 1 1.15 1.15 1.22 1.22
#> 2 auburn, grey 1 1.15 2.30 1.22 2.44
#> 3 auburn, white 1 1.15 3.45 1.22 3.66
#> 4 black 13 14.9 18.4 15.9 19.5
#> 5 blond 3 3.45 21.8 3.66 23.2
#> 6 blonde 1 1.15 23.0 1.22 24.4
#> 7 brown 18 20.7 43.7 22.0 46.3
#> 8 brown, grey 1 1.15 44.8 1.22 47.6
#> 9 grey 1 1.15 46.0 1.22 48.8
#> 10 none 37 42.5 88.5 45.1 93.9
#> 11 unknown 1 1.15 89.7 1.22 95.1
#> 12 white 4 4.60 94.3 4.88 100
#> 13 NA 5 5.75 100 NA NA
#>
#> $skin_color
#> # A tibble: 31 × 6
#> value n percent cum valid valid_cum
#> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 blue 2 2.30 2.30 2.30 2.30
#> 2 blue, grey 2 2.30 4.60 2.30 4.60
#> 3 brown 4 4.60 9.20 4.60 9.20
#> 4 brown mottle 1 1.15 10.3 1.15 10.3
#> 5 brown, white 1 1.15 11.5 1.15 11.5
#> 6 dark 6 6.90 18.4 6.90 18.4
#> 7 fair 17 19.5 37.9 19.5 37.9
#> 8 fair, green, yellow 1 1.15 39.1 1.15 39.1
#> 9 gold 1 1.15 40.2 1.15 40.2
#> 10 green 6 6.90 47.1 6.90 47.1
#> # ℹ 21 more rows
#>
#> $eye_color
#> # A tibble: 15 × 6
#> value n percent cum valid valid_cum
#> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 black 10 11.5 11.5 11.5 11.5
#> 2 blue 19 21.8 33.3 21.8 33.3
#> 3 blue-gray 1 1.15 34.5 1.15 34.5
#> 4 brown 21 24.1 58.6 24.1 58.6
#> 5 dark 1 1.15 59.8 1.15 59.8
#> 6 gold 1 1.15 60.9 1.15 60.9
#> 7 green, yellow 1 1.15 62.1 1.15 62.1
#> 8 hazel 3 3.45 65.5 3.45 65.5
#> 9 orange 8 9.20 74.7 9.20 74.7
#> 10 pink 1 1.15 75.9 1.15 75.9
#> 11 red 5 5.75 81.6 5.75 81.6
#> 12 red, blue 1 1.15 82.8 1.15 82.8
#> 13 unknown 3 3.45 86.2 3.45 86.2
#> 14 white 1 1.15 87.4 1.15 87.4
#> 15 yellow 11 12.6 100 12.6 100
#>
tab1(starwars, ends_with("color"), .type = "tibble")
#> # A tibble: 59 × 7
#> variable value n percent cum valid valid_cum
#> <chr> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 hair_color auburn 1 1.15 1.15 1.22 1.22
#> 2 hair_color auburn, grey 1 1.15 2.30 1.22 2.44
#> 3 hair_color auburn, white 1 1.15 3.45 1.22 3.66
#> 4 hair_color black 13 14.9 18.4 15.9 19.5
#> 5 hair_color blond 3 3.45 21.8 3.66 23.2
#> 6 hair_color blonde 1 1.15 23.0 1.22 24.4
#> 7 hair_color brown 18 20.7 43.7 22.0 46.3
#> 8 hair_color brown, grey 1 1.15 44.8 1.22 47.6
#> 9 hair_color grey 1 1.15 46.0 1.22 48.8
#> 10 hair_color none 37 42.5 88.5 45.1 93.9
#> # ℹ 49 more rows
fre1(starwars, ends_with("color"))
variable
value
n
percent
cum
valid
valid_cum
character
character
integer
numeric
numeric
numeric
numeric
hair_color
auburn
1
1.1
1.1
1.2
1.2
hair_color
auburn, grey
1
1.1
2.3
1.2
2.4
hair_color
auburn, white
1
1.1
3.4
1.2
3.7
hair_color
black
13
14.9
18.4
15.9
19.5
hair_color
blond
3
3.4
21.8
3.7
23.2
hair_color
blonde
1
1.1
23.0
1.2
24.4
hair_color
brown
18
20.7
43.7
22.0
46.3
hair_color
brown, grey
1
1.1
44.8
1.2
47.6
hair_color
grey
1
1.1
46.0
1.2
48.8
hair_color
none
37
42.5
88.5
45.1
93.9
hair_color
unknown
1
1.1
89.7
1.2
95.1
hair_color
white
4
4.6
94.3
4.9
100.0
hair_color
Valid Total
82
94.3
100.0
hair_color
NA
5
5.7
100.0
hair_color
Total
87
100.0
skin_color
blue
2
2.3
2.3
2.3
2.3
skin_color
blue, grey
2
2.3
4.6
2.3
4.6
skin_color
brown
4
4.6
9.2
4.6
9.2
skin_color
brown mottle
1
1.1
10.3
1.1
10.3
skin_color
brown, white
1
1.1
11.5
1.1
11.5
skin_color
dark
6
6.9
18.4
6.9
18.4
skin_color
fair
17
19.5
37.9
19.5
37.9
skin_color
fair, green, yellow
1
1.1
39.1
1.1
39.1
skin_color
gold
1
1.1
40.2
1.1
40.2
skin_color
green
6
6.9
47.1
6.9
47.1
skin_color
green, grey
1
1.1
48.3
1.1
48.3
skin_color
green-tan, brown
1
1.1
49.4
1.1
49.4
skin_color
grey
6
6.9
56.3
6.9
56.3
skin_color
grey, blue
1
1.1
57.5
1.1
57.5
skin_color
grey, green, yellow
1
1.1
58.6
1.1
58.6
skin_color
grey, red
1
1.1
59.8
1.1
59.8
skin_color
light
11
12.6
72.4
12.6
72.4
skin_color
metal
1
1.1
73.6
1.1
73.6
skin_color
mottled green
1
1.1
74.7
1.1
74.7
skin_color
none
1
1.1
75.9
1.1
75.9
skin_color
orange
2
2.3
78.2
2.3
78.2
skin_color
pale
5
5.7
83.9
5.7
83.9
skin_color
red
1
1.1
85.1
1.1
85.1
skin_color
red, blue, white
1
1.1
86.2
1.1
86.2
skin_color
silver, red
1
1.1
87.4
1.1
87.4
skin_color
tan
2
2.3
89.7
2.3
89.7
skin_color
unknown
2
2.3
92.0
2.3
92.0
skin_color
white
2
2.3
94.3
2.3
94.3
skin_color
white, blue
2
2.3
96.6
2.3
96.6
skin_color
white, red
1
1.1
97.7
1.1
97.7
skin_color
yellow
2
2.3
100.0
2.3
100.0
skin_color
Total
87
100.0
eye_color
black
10
11.5
11.5
11.5
11.5
eye_color
blue
19
21.8
33.3
21.8
33.3
eye_color
blue-gray
1
1.1
34.5
1.1
34.5
eye_color
brown
21
24.1
58.6
24.1
58.6
eye_color
dark
1
1.1
59.8
1.1
59.8
eye_color
gold
1
1.1
60.9
1.1
60.9
eye_color
green, yellow
1
1.1
62.1
1.1
62.1
eye_color
hazel
3
3.4
65.5
3.4
65.5
eye_color
orange
8
9.2
74.7
9.2
74.7
eye_color
pink
1
1.1
75.9
1.1
75.9
eye_color
red
5
5.7
81.6
5.7
81.6
eye_color
red, blue
1
1.1
82.8
1.1
82.8
eye_color
unknown
3
3.4
86.2
3.4
86.2
eye_color
white
1
1.1
87.4
1.1
87.4
eye_color
yellow
11
12.6
100.0
12.6
100.0
eye_color
Total
87
100.0
n: 65
fre1(starwars, ends_with("color"), .type = "list")
#> $hair_color
#> # A tibble: 15 × 6
#> value n percent cum valid valid_cum
#> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 auburn 1 1.15 1.15 1.22 1.22
#> 2 auburn, grey 1 1.15 2.30 1.22 2.44
#> 3 auburn, white 1 1.15 3.45 1.22 3.66
#> 4 black 13 14.9 18.4 15.9 19.5
#> 5 blond 3 3.45 21.8 3.66 23.2
#> 6 blonde 1 1.15 23.0 1.22 24.4
#> 7 brown 18 20.7 43.7 22.0 46.3
#> 8 brown, grey 1 1.15 44.8 1.22 47.6
#> 9 grey 1 1.15 46.0 1.22 48.8
#> 10 none 37 42.5 88.5 45.1 93.9
#> 11 unknown 1 1.15 89.7 1.22 95.1
#> 12 white 4 4.60 94.3 4.88 100
#> 13 Valid Total 82 94.3 NA 100 NA
#> 14 NA 5 5.75 100 NA NA
#> 15 Total 87 100 NA NA NA
#>
#> $skin_color
#> # A tibble: 32 × 6
#> value n percent cum valid valid_cum
#> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 blue 2 2.30 2.30 2.30 2.30
#> 2 blue, grey 2 2.30 4.60 2.30 4.60
#> 3 brown 4 4.60 9.20 4.60 9.20
#> 4 brown mottle 1 1.15 10.3 1.15 10.3
#> 5 brown, white 1 1.15 11.5 1.15 11.5
#> 6 dark 6 6.90 18.4 6.90 18.4
#> 7 fair 17 19.5 37.9 19.5 37.9
#> 8 fair, green, yellow 1 1.15 39.1 1.15 39.1
#> 9 gold 1 1.15 40.2 1.15 40.2
#> 10 green 6 6.90 47.1 6.90 47.1
#> # ℹ 22 more rows
#>
#> $eye_color
#> # A tibble: 16 × 6
#> value n percent cum valid valid_cum
#> <chr> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 black 10 11.5 11.5 11.5 11.5
#> 2 blue 19 21.8 33.3 21.8 33.3
#> 3 blue-gray 1 1.15 34.5 1.15 34.5
#> 4 brown 21 24.1 58.6 24.1 58.6
#> 5 dark 1 1.15 59.8 1.15 59.8
#> 6 gold 1 1.15 60.9 1.15 60.9
#> 7 green, yellow 1 1.15 62.1 1.15 62.1
#> 8 hazel 3 3.45 65.5 3.45 65.5
#> 9 orange 8 9.20 74.7 9.20 74.7
#> 10 pink 1 1.15 75.9 1.15 75.9
#> 11 red 5 5.75 81.6 5.75 81.6
#> 12 red, blue 1 1.15 82.8 1.15 82.8
#> 13 unknown 3 3.45 86.2 3.45 86.2
#> 14 white 1 1.15 87.4 1.15 87.4
#> 15 yellow 11 12.6 100 12.6 100
#> 16 Total 87 100 NA NA NA
#>
tab2(starwars, sex, gender)
#> # A tibble: 5 × 4
#> `sex \\ gender` feminine masculine `NA`
#> <chr> <int> <int> <int>
#> 1 female 16 0 0
#> 2 hermaphroditic 0 1 0
#> 3 male 0 60 0
#> 4 none 1 5 0
#> 5 NA 0 0 4
tab2(starwars, height, gender)
#> # A tibble: 46 × 4
#> `height \\ gender` masculine feminine `NA`
#> <int> <int> <int> <int>
#> 1 66 1 0 0
#> 2 79 1 0 0
#> 3 88 1 0 0
#> 4 94 1 0 0
#> 5 96 1 1 0
#> 6 97 1 0 0
#> 7 112 1 0 0
#> 8 122 1 0 0
#> 9 137 1 0 0
#> 10 150 0 2 0
#> # ℹ 36 more rows
fre2(starwars, height, gender)
height \ gender
masculine
feminine
NA
total
character
integer
integer
integer
integer
66
1
0
0
1
79
1
0
0
1
88
1
0
0
1
94
1
0
0
1
96
1
1
0
2
97
1
0
0
1
112
1
0
0
1
122
1
0
0
1
137
1
0
0
1
150
0
2
0
2
157
0
1
0
1
160
1
0
0
1
163
1
1
0
2
165
0
3
0
3
166
0
1
0
1
167
1
1
0
2
168
0
1
0
1
170
3
1
0
4
171
1
0
0
1
172
1
0
0
1
173
1
0
0
1
175
3
0
0
3
177
1
0
0
1
178
1
2
1
4
180
5
0
0
5
182
1
0
0
1
183
5
0
2
7
184
0
1
0
1
185
1
0
0
1
188
5
0
0
5
190
1
0
0
1
191
3
0
0
3
193
3
0
0
3
196
3
0
0
3
198
2
0
0
2
200
1
0
0
1
202
1
0
0
1
206
2
0
0
2
213
0
1
0
1
216
1
0
0
1
224
1
0
0
1
228
1
0
0
1
229
1
0
0
1
234
1
0
0
1
264
1
0
0
1
NA
4
1
1
6
total
66
17
4
87
n: 47