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

Thousand separator (i.e ",") #43

Open
linusgun opened this issue Jul 21, 2023 · 3 comments
Open

Thousand separator (i.e ",") #43

linusgun opened this issue Jul 21, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@linusgun
Copy link

Describe the new feature
Most of the time when I am dealing with number of the range of thousands, reducing that numbers with scientific notation is too much, so having a thousand separator would improve the readiness of that numbers.

Known workaround
I am just accepting as it is.

@linusgun linusgun added the enhancement New feature or request label Jul 21, 2023
@DanChaltiel
Copy link
Owner

Good idea!
I'm not sure how I can do that simply though, because number formatting is buried far inside the functions.

In the meantime, you could also use custom functions like this:

library(tidyverse)
library(crosstable)
f = scales::label_dollar(accuracy=0.1, prefix="")
iris %>% 
  select(starts_with("Sepal")) %>% 
  mutate_all(~.x*10000) %>% 
  crosstable(funs=c(mean=\(x) f(mean(x)), sd=\(x) f(sd(x))))
#> # A tibble: 4 x 4
#>   .id          label        variable value   
#>   <chr>        <chr>        <chr>    <chr>   
#> 1 Sepal.Length Sepal.Length mean     58,433.3
#> 2 Sepal.Length Sepal.Length sd       8,280.7 
#> 3 Sepal.Width  Sepal.Width  mean     30,573.3
#> 4 Sepal.Width  Sepal.Width  sd       4,358.7

Created on 2023-07-21 with reprex v2.0.2

@linusgun
Copy link
Author

linusgun commented Sep 1, 2023

I just realized that it doesn't work for counting categorical variables:

f = scales::label_dollar(accuracy=0.1, prefix="")
iris %>% 
  slice(rep(1:n(), each = 100)) %>% 
  select(Species) %>% 
  crosstable(funs=c(count=\(x) f(count(x))))

# A tibble: 3 × 4
  .id     label   variable   value        
  <chr>   <chr>   <chr>      <chr>        
1 Species Species setosa     5000 (33.33%)
2 Species Species versicolor 5000 (33.33%)
3 Species Species virginica  5000 (33.33%)

@DanChaltiel
Copy link
Owner

funs is for control over numeric variables.
IMHO, it wouldn't make much sense to harmonize formatting on numeric and categorical variables.
For categorical variables, you cannot have as much control but you can still use percent_digits and percent_pattern.
For instance:

iris %>% 
   slice(rep(1:n(), each = 100)) %>% 
   select(Species) %>% 
   crosstable(percent_pattern="N={n} (P={p_col})", 
              percent_digits=1)
# A tibble: 3 x 4
  .id     label   variable   value           
  <chr>   <chr>   <chr>      <chr>           
1 Species Species setosa     N=5000 (P=33.3%)
2 Species Species versicolor N=5000 (P=33.3%)
3 Species Species virginica  N=5000 (P=33.3%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants