Skip to content

Commit

Permalink
tabyl() does not call left_join() due to breaking changes in dplyr 0.6.0
Browse files Browse the repository at this point in the history
NA no longer joined to NA.  Closes #111
  • Loading branch information
sfirke committed Apr 17, 2017
1 parent 866cb81 commit 9a441a7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion R/tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ tabyl.default <- function(vec, sort = FALSE, show_na = TRUE, ...) {
result <- dat %>% dplyr::count(vec, sort = sort)

if(is.factor(vec)){
result <- tidyr::complete(result, vec)
expanded <- tidyr::expand(result, vec)
result <- merge(x = expanded, # can't use dplyr::left_join because as of 0.6.0, NAs don't match, and na_matches argument not present < 0.6.0
y = result,
by = "vec",
all.x = TRUE)
result <- dplyr::arrange(result, vec) # restore sorting by factor level
if(sort){result <- dplyr::arrange(result, dplyr::desc(n))} # undo reorder caused by complete()
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test_that("show_NA = FALSE parameter works", {
tabyl(test_df_na$grp, show_na = FALSE))
})

test_that("sorting is preserved for factors", {
test_that("ordering of result by factor levels is preserved for factors", {
expect_equal(tabyl(factor(c("x", "y", "z"), levels = c("y", "z", "x")))[[1]], factor(c("y", "z", "x"), levels = c("y", "z", "x")))
})

Expand Down

0 comments on commit 9a441a7

Please sign in to comment.