-
Notifications
You must be signed in to change notification settings - Fork 133
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
extend dplyr to include tabyl class so that tabyl attributes are preserved by dplyr operations #527
Comments
Could you share more about how df2 gets created? I see your object has class tabyl but it does not have the attributes "core" or "tabyl_type" which a tabyl should. The new I could start exploring how to mitigate this - but I would need to understand how your input is created, since I can't conceive of a way that someone is getting an object that's a |
Hello, 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
library(janitor)
#>
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#>
#> chisq.test, fisher.test
df <- structure(list(member_state_3_letter_codes = c("AUT", "AUT",
"AUT", "AUT", "AUT", "AUT", "AUT", "AUT", "AUT", "AUT"), case_no = c("N 135/2010",
"N 197/2010", "N 418/2007", "N 521/2009", "N 564b/2004", "N 622/2003",
"SA.100539", "SA.32485", "SA.33384", "SA.33496"), year = c(2021,
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021), amount_spent_aid_element_in_eur_million = c(4.831,
0.25, 0.01, 0.004, 0.5668, 0.2883, 0.005, 0.981, 314.074, 0.042
), procedure_name = c("Notified Aid", "Notified Aid", "Notified Aid",
"Notified Aid", "Notified Aid", "Notified Aid", "General Block Exemption Regulation",
"General Block Exemption Regulation", "Notified Aid", "Notified Aid"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
df2 <- df |>
tabyl(procedure_name) |>
## mutate(percent=round_preserve_sum(percent*100,1)) %>%
adorn_totals()
df2
#> procedure_name n percent
#> General Block Exemption Regulation 2 0.2
#> Notified Aid 8 0.8
#> Total 10 1.0
df3 <- df |>
tabyl(procedure_name) |>
mutate(percent=round(percent*100,1)) |>
adorn_totals()
#> Error in 1:nrow(attr(dat, "core")): argument of length 0
df3
#> Error in eval(expr, envir, enclos): object 'df3' not found Created on 2023-02-03 with reprex v2.0.2 |
Interesting. Beginning Attributes
Attributes after
I see how this change in 2.2.0 could feel like a regression to you but it was accidental that it worked in the first place. And the new behavior is as a result of an improvement in janitor. Perhaps I could mitigate this on the janitor side, but not right now - I'm tired from the last rewrite and this feels like it's caused by another package. For now here is the janitor-y way to do what you're doing:
Or if you want an integer output multiplied by 100, then simply do that |
Thanks. Beyond the case in the reprex, for which you show an elegant solution, I may want to call tabyl, mutate all sort of stuff in the ensuing object and then calling adorn_totals. |
|
In this discussion, Davis Vaughan provided good insight into what's going on here and how janitor could extend dplyr such that attributes would be preserved. I do not have the bandwidth to implement this anytime soon unfortunately, but there's the full scoop on the situation. |
Hello,
In my workflow I often call tabyl on some data and as a consequence I get an object having both "tabyl" and "data.frame" class.
It seems to me that if the resulting object inherits the "tabyl" class then adorn_totals will fail on it.
Why? I think I did not have this in the past.
In the reprex below, df has only the "data.frame" class and everything is fine.
df2 has also the tabyl class (in reality it comes from calling tabyl on some data) and then adorn_totals() fails.
Any idea if this is intended?
Thanks!
Created on 2023-02-03 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: