Skip to content

Commit

Permalink
address breaking change in dplyr::last() (#491)
Browse files Browse the repository at this point in the history
Fix adorn_percentages() and close #490
  • Loading branch information
sfirke authored Oct 21, 2022
1 parent 469ecfe commit 8d1573c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

## Bug fixes

* `adorn_percentages()` was refactored for compatibility with `dplyr` package versions > 1.0.99 (#490)

* When a numeric variable is supplied as the 2nd variable (column) or 3rd variable (list) of a `tabyl`, the resulting columns or list are now sorted in numeric order, not alphabetic. (#438, thanks **@daaronr** for reporting and **@mattroumaya** for fixing)

* `tabyl()` now succeeds when the second variable is named `"n"` (#445).
Expand Down
4 changes: 2 additions & 2 deletions R/adorn_percentages.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ adorn_percentages <- function(dat, denominator = "row", na.rm = TRUE, ...) {
if ("col" %in% attr(dat, "totals") & !explicitly_exempt_totals) {
cols_to_tally <- c(cols_to_tally, ncol(dat))
if ("row" %in% attr(dat, "totals")) {
col_sum <- c(col_sum, sum(dplyr::last(dat)[-nrow(dat)]))
col_sum <- c(col_sum, sum(dat[-nrow(dat), ncol(dat)]))
} else {
col_sum <- c(col_sum, sum(dplyr::last(dat)))
col_sum <- c(col_sum, sum(dat[ , ncol(dat)]))
}
}
dat[cols_to_tally] <- sweep(dat[cols_to_tally], 2, col_sum, `/`) # from http://stackoverflow.com/questions/9447801/dividing-columns-by-colsums-in-r
Expand Down

0 comments on commit 8d1573c

Please sign in to comment.