Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: order splits alphabetically in qtable for character vars #710

Open
3 tasks done
anajens opened this issue Aug 11, 2023 · 1 comment
Open
3 tasks done

Comments

@anajens
Copy link
Contributor

anajens commented Aug 11, 2023

Feature description

When using qtable with character variables it would be a nice feature if the row and column splits are ordered alphabetically. This would help improve readability of the table with multiple categories. Also this would follow the behaviour of dplyr::count.

> library(rtables)
Loading required package: formatters
Loading required package: magrittr
> library(dplyr)

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

> 
> set.seed(123)
> tmp <- data.frame(
+   x = rep("x", 50),
+   y = sample(letters[1:5], size = 50, replace = TRUE, prob = c(0.1, 0.2, 0.3, 0.35, 0.05)),
+   stringsAsFactors = FALSE
+ )
> 
> # Categories are not sorted:
> qtable(tmp, "y")
count   all obs
        (N=50) 
———————————————
d         17   
b          8   
c         14   
a          7   
e          4   
> 
> # dplyr sorts:
> tmp %>% count(y)
  y  n
1 a  7
2 b  8
3 c 14
4 d 17
5 e  4

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.
@gmbecker
Copy link
Collaborator

gmbecker commented Aug 11, 2023

This makes sense in isolation but I'm not sure it does within the larger scheme of things, because the rtables framework as a whole matches what happens now (implicit levels of character vectors are ordered by their first appearance in the full dataset, and then fixed in that order regardless of faceting hierarchy).

So while i think this would be nice for qtable and match it's purpose well, I'm not sure it's behavior should diverge from the core tabulation engine, and changing that that might be desirable but would be a fairly large breaking change we would need to think hard before implementing.

Lets discuss this more before we implement it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants