sfirke/janitor

tabyl() suggestion: sorting

Closed this issue · 3 comments

I was thinking it might be nice to have an argument in tabyl to adjust the sorting. For instance, sorting in ascending or descending order by n, or sorting in alphabetical order by the row variable.

Trying to do this using the standard tidyverse arrange() after the tabyl is generated doesn't work if there is a totals row.

A sort argument was actually in the earliest version of tabyl, until v 0.3.1, and got removed for 1.0.0. #192 has a little history of it. In short, dplyr::arrange() gets the job done with a quick short line and then it's one less argument to tabyl -- an argument that is hard to make sensible across both 1- and 2-way tabyls.

I do want to work on specific problems though like the one you flagged (and this conversation had me find another problem that I'll open an issue for in a minute!). I see the problem you're talking about, but would a sort argument to tabyl help if you then add the adorn_totals() after that?

Ah yes you are right, just doing the arrange() before adorn_totals() gets the desired output, I'm not sure why that didn't occur to me!

And I can see how that would be super messy for more than one-way tables since likely you'd want each table to be in the same categorical order. I think that using arrange() first is all the solution this requires at present.

Okay then I'll close this. Appreciate the idea!