diff --git a/NEWS.md b/NEWS.md index 3fdbd13a..4b9adc59 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/adorn_percentages.R b/R/adorn_percentages.R index d8ceb534..7f193b64 100644 --- a/R/adorn_percentages.R +++ b/R/adorn_percentages.R @@ -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