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

Issue with weighted percentage in flat_table() #151

Open
realrbird opened this issue May 12, 2022 · 0 comments
Open

Issue with weighted percentage in flat_table() #151

realrbird opened this issue May 12, 2022 · 0 comments

Comments

@realrbird
Copy link

library(tidyverse)
library(sjmisc)

df <- mtcars %>%
  mutate(cyl = as.factor(cyl), gear = as.factor(gear)) %>%
  select(cyl, gear, mpg) %>%
  na.omit() %>%
  as_tibble()

# Using sjmisc
flat_table(df, cyl, gear, weights = mpg, margin = 'col', digits = 9)
#>     gear         3         4         5
#> cyl                                   
#> 4         9.053498 73.129252 52.336449
#> 6        16.460905 26.870748 18.691589
#> 8        74.485597  0.000000 28.971963

# using dplyr
count(df, cyl, gear, .drop = FALSE, wt = mpg) %>%
  group_by(gear) %>%
  mutate(n = (n/sum(n)) * 100) %>%
  pivot_wider(names_from = gear, values_from = n) %>%
  as.data.frame()
#>   cyl         3        4        5
#> 1   4  8.899007 73.16576 52.75959
#> 2   6 16.349338 26.83424 18.42844
#> 3   8 74.751656  0.00000 28.81197

# WORKS FINE WITHOUT WEIGHTS
flat_table(df, cyl, gear, margin = 'col', digits = 9)
#>     gear         3         4         5
#> cyl                                   
#> 4         6.666667 66.666667 40.000000
#> 6        13.333333 33.333333 20.000000
#> 8        80.000000  0.000000 40.000000

count(df, cyl, gear, .drop = FALSE) %>%
  group_by(gear) %>%
  mutate(n = (n/sum(n)) * 100) %>%
  pivot_wider(names_from = gear, values_from = n) %>%
  as.data.frame()
#>   cyl         3        4  5
#> 1   4  6.666667 66.66667 40
#> 2   6 13.333333 33.33333 20
#> 3   8 80.000000  0.00000 40

Created on 2022-05-12 by the reprex package (v2.0.1)

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

No branches or pull requests

1 participant