Skip to content

Commit

Permalink
move NA_ column to the right-most position in crosstab
Browse files Browse the repository at this point in the history
closes #109
  • Loading branch information
sfirke committed May 5, 2017
1 parent 700ddd0 commit 71446a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions R/crosstab.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ crosstab.default <- function(vec1, vec2, percent = "none", show_na = TRUE, ...){
tidyr::spread_(dat_col_names[[2]], "n", fill = 0) %>%
dplyr::ungroup()

if("NA_" %in% names(result)){ result <- dplyr::select(result, -NA_, everything()) } # move NA_ column to end
# calculate percentages, if specified
if(percent != "none"){result <- ns_to_percents(result, denom = percent)}

Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-crosstab.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,15 @@ expect_equal(
"dat$`The candidate(s) applied directly to my school` %>% gsub(\"hi\", \"there\", .)"
)
})


test_that("NA character column is displayed in right-most position", {
dat <- data.frame(
a = c(1, 1),
b = c("x", NA),
stringsAsFactors = FALSE
)
expect_equal(dat %>% crosstab(a, b),
data.frame(a = 1, x = 1, NA_ = 1)
)
})

0 comments on commit 71446a4

Please sign in to comment.