Skip to content

Commit

Permalink
Sort 3 way tabyl factor levels (#287)
Browse files Browse the repository at this point in the history
closes #250, three-way tabyls where 3rd variable is a factor are now sorted by factor level
  • Loading branch information
sfirke authored Apr 18, 2019
1 parent de409fa commit f44b71e
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 1,258 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ Package: janitor
Title: Simple Tools for Examining and Cleaning Dirty Data
Version: 1.1.1.9000
Authors@R: c(person("Sam", "Firke", email = "[email protected]", role = c("aut", "cre")),
person("Bill", "Denney", email = "[email protected]", role = "ctb"),
person("Chris", "Haid", email = "[email protected]", role = "ctb"),
person("Ryan", "Knight", email = "[email protected]", role = "ctb"),
person("Bill", "Denney", email = "[email protected]", role = "ctb"),
person("Malte", "Grosser", email = "[email protected]", role = "ctb"))
Description: The main janitor functions can: perfectly format data.frame column
names; provide quick counts of variable combinations (i.e., frequency
Expand Down
9 changes: 8 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ Two new function `janitor::chisq.test()` and `janitor::fisher.test()` allow to a

* `adorn_totals()` gains an argument `"name"` that allows the user to specify a value other than "Total" to appear as the name of the added row and/or column. (#263) Thanks to **@StephieLaPugh** for suggesting and **@daniel-barnett** for implementing.

* `remove_empty()` now works with matrices (returning a matrix). (#215) Thanks to **@jsta** for reporting and **@billdenney** for patching.

* If the third variable in a three-way tabyl is a factor, the resulting list is sorted in order of its levels (#250). Empty factor levels in the 3rd variable are still omitted regardless of the value of `show_missing_levels`.

## Bug fixes

* `remove_empty()` now works with matrices (returning a matrix). (#215) Thanks to **@jsta** for reporting and **@billdenney** for patching.

* `excel_numeric_to_date()` no longer gives an overflow error for integer input (for dates since 1968). (#241) Thanks to **@hideaki** for reporting and **@billdenney** for patching.

* `clean_names()` and `make_clean_names()` now support 'none' as a case option. (#269) Thanks to **@andrewbarros** for reporting and patching.


# janitor 1.1.1 (2018-07-30)

## Release summary
Expand All @@ -34,6 +40,7 @@ Patches a bug introduced in version 1.1.0 where `excel_numeric_to_date()` would

* `excel_numeric_to_date()` again handles `NA` correctly, in version 1.1.0 the function would error if any values of the input vector were `NA`. (#220). Thanks **@emilelatour** for reporting and **@billdenney** for patching.


# janitor 1.1.0 (2018-07-17)

## Release summary
Expand Down
13 changes: 13 additions & 0 deletions R/tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ tabyl_2way <- function(dat, var1, var2, show_na = TRUE, show_missing_levels = TR
# a list of two-way frequency tables, split into a list on a third variable
tabyl_3way <- function(dat, var1, var2, var3, show_na = TRUE, show_missing_levels = TRUE) {
dat <- dplyr::select(dat, !! var1, !! var2, !! var3)

# Keep factor levels for ordering the list at the end
if(is.factor(dat[[3]])){
third_levels_for_sorting <- levels(dat[[3]])
}
dat[[3]] <- as.character(dat[[3]]) # don't want empty factor levels in the result list - they would be empty data.frames

# grab class of 1st variable to restore it later
Expand All @@ -240,6 +245,9 @@ tabyl_3way <- function(dat, var1, var2, var3, show_na = TRUE, show_missing_level
if (show_na && sum(is.na(dat[[3]])) > 0) {
dat[[3]] <- factor(dat[[3]], levels = c(sort(unique(dat[[3]])), "NA_"))
dat[[3]][is.na(dat[[3]])] <- "NA_"
if(exists("third_levels_for_sorting")){
third_levels_for_sorting <- c(third_levels_for_sorting, "NA_")
}
}

if (!show_missing_levels) { # this shows missing factor levels, to make the crosstabs consistent across each data.frame in the list based on values of var3
Expand All @@ -258,6 +266,11 @@ tabyl_3way <- function(dat, var1, var2, var3, show_na = TRUE, show_missing_level
purrr::map(tabyl_2way, var1, var2, show_na = show_na, show_missing_levels = show_missing_levels) %>%
purrr::map(reset_1st_col_status, col1_class, col1_levels) # reset class of var in 1st col to its input class, #168

# reorder when var 3 is a factor, per #250
if(exists("third_levels_for_sorting")){
result <- result[order(third_levels_for_sorting[third_levels_for_sorting %in% unique(dat[[3]])])]
}

result
}

Expand Down
48 changes: 0 additions & 48 deletions inst/doc/introduction.R

This file was deleted.

121 changes: 0 additions & 121 deletions inst/doc/introduction.Rmd

This file was deleted.

154 changes: 0 additions & 154 deletions inst/doc/introduction.html

This file was deleted.

51 changes: 0 additions & 51 deletions inst/doc/janitor.R

This file was deleted.

Loading

0 comments on commit f44b71e

Please sign in to comment.