-
Notifications
You must be signed in to change notification settings - Fork 130
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
adorn_ns() adds excluded values to a adorn_totals() in a pipe #533
Comments
Hello! I don't see a way to pass along that information from That last execution looks good to me. Can you say more about:
Can you show what you were expecting in that last execution? |
Thanks for your respond and clarification. Sorry for being ambiguous. At least in the last example I would expect the same results as the in the first: example %>%
adorn_totals(c("col", "row"), fill = "-", na.rm = TRUE, name = "Total", !c(year, ignored_num)) %>%
adorn_percentages() %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns(,,,!c(year, ignored_num))
# I get:
#> year var1 var2 ignored_num Total
#> 2019 62.5% (20) 37.5% (12) 99 100.0% (131)
#> 2020 73.2% (30) 26.8% (11) 99 100.0% (140)
#> Total 68.5% (50) 31.5% (23) - 100.0% (271)
# I'd expect:
# year var1 var2 ignored_num Total
# 2019 62.5% (20) 37.5% (12) 99 100.0% (32)
# 2020 73.2% (30) 26.8% (11) 99 100.0% (41)
# Total 68.5% (50) 31.5% (23) - 100.0% (73) So with I would expect N.B.: The documentation states that the first column and all non-numeric columns are always ignored. So I guess, I have to cast the ignored_num as character. My understanding of the docs is that I could exclude (numeric) columns via tidyselect specifications as well:
Might be misinterpretation. |
Hello, is there anything new on that subject? I created a minimal example to pinpoint what's unexpected: library(tidyverse)
library(janitor)
# For the sake of the example, month is numeric.
example <- tribble(
~year, ~month, ~var1, ~var2,
2019,11,20,10,
2020,12,30,10)
# I aim to get the column totals of var1 and var2. (Ignore year and month.)
# Those should be *30* and *40*
# Attempt 1: Do nothing (not expecting the intended results)
example %>%
adorn_totals(c("col", "row"), fill = "-", na.rm = TRUE, name = "Total") %>%
adorn_ns()
#> year month var1 var2 Total
#> 2019 11 (11) 20 (20) 10 (10) 41 (41)
#> 2020 12 (12) 30 (30) 10 (10) 52 (52)
#> Total 23 (23) 50 (50) 20 (20) 93 (93)
# Obviously this didn't work, because month is numeric and gets added up too.
# I get 30+11 = 41 and 40+12 = 52.
# So far so good (and correct).
# Attempt 2: Exclude month from _totals and fom _ns:
example %>%
adorn_totals(c("col", "row"), fill = "-", na.rm = TRUE, name = "Total", !c(year, month)) %>%
adorn_ns(,,,!c(year, month))
#> year month var1 var2 Total
#> 2019 11 20 (20) 10 (10) 30 (41)
#> 2020 12 30 (30) 10 (10) 40 (52)
#> Total - 50 (50) 20 (20) 70 (93)
# Now the _ns in the Total column are wrong: (41) instead of (30), (52) instead of (40)
# Even though we excluded the month column.
# Attempt 3:
# Of course, when I convert the month to something non-numeric, it works:
example <- example %>% mutate(month = as.factor(month))
example %>%
adorn_totals(c("col", "row"), fill = "-", na.rm = TRUE, name = "Total") %>%
adorn_ns()
#> year month var1 var2 Total
#> 2019 11 20 (20) 10 (10) 30 (30)
#> 2020 12 30 (30) 10 (10) 40 (40)
#> Total - 50 (50) 20 (20) 70 (70)
# This is the result I expected when excluding month. Attempt 2 is what puzzles me: I exclude one numeric column but this exclusion is partly ignored in the totals column (see highlighted numbers).
|
It might be correct, but hit me by surprise.
Excluding a variable in adorn_totals() works as expected, but adding percentages and Ns later on in the pipe reintroduces the excluded values.
Studying the other issues, it reminds me of #195
Created on 2023-03-14 by the reprex package (v2.0.1)
The text was updated successfully, but these errors were encountered: