Title

pivot_count(data, cols = NULL, rows = NULL, pivot = T, wt = NULL)

Arguments

data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.

cols

a character vector of items

rows

a character vector of items

pivot

logical: should wide table be returned - col categories as columns (TRUE), or left long and tidy (FALSE)?

Value

Examples

tidy_titanic %>% pivot_count(rows = sex)
#> # A tibble: 2 × 2 #> sex count #> <fct> <dbl> #> 1 Male 1731 #> 2 Female 470
tidy_titanic %>% pivot_count(cols = sex)
#> # A tibble: 1 × 2 #> Male Female #> <dbl> <dbl> #> 1 1731 470
tidy_titanic %>% pivot_count(rows = survived, cols = sex)
#> # A tibble: 2 × 3 #> survived Male Female #> <fct> <dbl> <dbl> #> 1 No 1364 126 #> 2 Yes 367 344
tidy_titanic %>% pivot_count(rows = c(survived, sex), cols = age)
#> # A tibble: 4 × 4 #> survived sex Child Adult #> <fct> <fct> <dbl> <dbl> #> 1 No Male 35 1329 #> 2 No Female 17 109 #> 3 Yes Male 29 338 #> 4 Yes Female 28 316
tidy_titanic %>% pivot_count(rows = c(survived), cols = c(age, sex))
#> # A tibble: 2 × 5 #> survived Child_Male Child_Female Adult_Male Adult_Female #> <fct> <dbl> <dbl> <dbl> <dbl> #> 1 No 35 17 1329 109 #> 2 Yes 29 28 338 316
tidy_titanic %>% pivot_count(cols = c(survived), rows = c(age, sex, class))
#> # A tibble: 16 × 5 #> age sex class No Yes #> <fct> <fct> <fct> <dbl> <dbl> #> 1 Child Male 1st 0 5 #> 2 Child Male 2nd 0 11 #> 3 Child Male 3rd 35 13 #> 4 Child Male Crew 0 0 #> 5 Child Female 1st 0 1 #> 6 Child Female 2nd 0 13 #> 7 Child Female 3rd 17 14 #> 8 Child Female Crew 0 0 #> 9 Adult Male 1st 118 57 #> 10 Adult Male 2nd 154 14 #> 11 Adult Male 3rd 387 75 #> 12 Adult Male Crew 670 192 #> 13 Adult Female 1st 4 140 #> 14 Adult Female 2nd 13 80 #> 15 Adult Female 3rd 89 76 #> 16 Adult Female Crew 3 20
tidy_titanic %>% pivot_count(rows = c(survived, sex), cols = age, pivot = F)
#> # A tibble: 8 × 4 #> age survived sex count #> <fct> <fct> <fct> <dbl> #> 1 Child No Male 35 #> 2 Child No Female 17 #> 3 Child Yes Male 29 #> 4 Child Yes Female 28 #> 5 Adult No Male 1329 #> 6 Adult No Female 109 #> 7 Adult Yes Male 338 #> 8 Adult Yes Female 316
flat_titanic %>% pivot_count(rows = sex, wt = freq)
#> # A tibble: 2 × 2 #> sex count #> <fct> <dbl> #> 1 Male 1731 #> 2 Female 470