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

Draft new data_summary() function #482

Merged
merged 38 commits into from
Mar 4, 2024
Merged

Draft new data_summary() function #482

merged 38 commits into from
Mar 4, 2024

Conversation

strengejacke
Copy link
Member

@strengejacke strengejacke commented Mar 3, 2024

Equvalent to dplyr::summarise()

library(datawizard)
data(iris)
data_summary(iris, MW = mean(Sepal.Width), SD = sd(Sepal.Width))
#>   MW |   SD
#> -----------
#> 3.06 | 0.44

data_summary(
  iris,
  MW = mean(Sepal.Width),
  SD = sd(Sepal.Width),
  by = "Species"
)
#> Species    |   MW |   SD
#> ------------------------
#> setosa     | 3.43 | 0.38
#> versicolor | 2.77 | 0.31
#> virginica  | 2.97 | 0.32

# same as
d <- data_group(iris, "Species")
data_summary(d, MW = mean(Sepal.Width), SD = sd(Sepal.Width))
#> Species    |   MW |   SD
#> ------------------------
#> setosa     | 3.43 | 0.38
#> versicolor | 2.77 | 0.31
#> virginica  | 2.97 | 0.32

# multiple groups
data(mtcars)
data_summary(mtcars, MW = mean(mpg), SD = sd(mpg), by = c("am", "gear"))
#> am | gear |    MW |   SD
#> ------------------------
#>  0 |    3 | 16.11 | 3.37
#>  0 |    4 | 21.05 | 3.07
#>  1 |    4 | 26.27 | 5.41
#>  1 |    5 | 21.38 | 6.66

# expressions can also be supplied as character strings
data_summary(mtcars, "MW = mean(mpg)", "SD = sd(mpg)", by = c("am", "gear"))
#> am | gear |    MW |   SD
#> ------------------------
#>  0 |    3 | 16.11 | 3.37
#>  0 |    4 | 21.05 | 3.07
#>  1 |    4 | 26.27 | 5.41
#>  1 |    5 | 21.38 | 6.66

Created on 2024-03-03 with reprex v2.1.0

Copy link

codecov bot commented Mar 3, 2024

Codecov Report

Attention: Patch coverage is 84.17722% with 25 lines in your changes are missing coverage. Please review.

Project coverage is 90.47%. Comparing base (83f9703) to head (af73c75).

Files Patch % Lines
R/categorize.R 56.75% 16 Missing ⚠️
R/adjust.R 53.84% 6 Missing ⚠️
R/data_summary.R 95.83% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #482      +/-   ##
==========================================
+ Coverage   90.34%   90.47%   +0.13%     
==========================================
  Files          73       74       +1     
  Lines        5696     5776      +80     
==========================================
+ Hits         5146     5226      +80     
  Misses        550      550              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@strengejacke
Copy link
Member Author

I moved some of the code from data_modify() in a separate function, to avoid duplicated code.

@strengejacke
Copy link
Member Author

Some examples with NA values:

library(datawizard)
data(efc, package = "datawizard")

data_summary(efc, MW = mean(c12hour, na.rm = TRUE), by = "c172code")
#> c172code |    MW
#> ----------------
#>        1 | 87.12
#>        2 | 94.05
#>        3 | 75.00
#>     <NA> | 47.80

data_summary(efc, MW = mean(c12hour, na.rm = TRUE), by = "c172code", include_na = FALSE)
#> c172code |    MW
#> ----------------
#>        1 | 87.12
#>        2 | 94.05
#>        3 | 75.00

data_summary(efc, MW = mean(c12hour, na.rm = TRUE), by = c("e42dep", "c172code"))
#> e42dep | c172code |     MW
#> --------------------------
#> 1      |        2 |  17.00
#> 2      |        2 |  34.25
#> 3      |        1 |  39.50
#> 3      |        2 |  52.44
#> 3      |        3 |  52.00
#> 3      |     <NA> |  84.00
#> 4      |        1 | 134.75
#> 4      |        2 | 119.26
#> 4      |        3 |  88.80
#> 4      |     <NA> |  43.29
#> <NA>   |        2 |   <NA>
#> <NA>   |     <NA> |   7.00

Created on 2024-03-03 with reprex v2.1.0

@DominiqueMakowski
Copy link
Member

Would be nice to make it compatible with our dataframe-returning functions, like ci() etc.

@strengejacke
Copy link
Member Author

strengejacke commented Mar 3, 2024

Would be nice to make it compatible with our dataframe-returning functions, like ci() etc.

See #483

I guess this would require special handling for these "exceptions" (separate handling for each special class). But let's first finish this PR.

@strengejacke
Copy link
Member Author

@etiennebacher if you want to review this PR, please wait until all checks pass.

@etiennebacher
Copy link
Member

Yes I'll review it in a couple of hours

@strengejacke
Copy link
Member Author

This reduced coverage is due to my lintr-fix in adjust.R, actually the code-coverage increased in this PR.

image

Copy link
Member

@etiennebacher etiennebacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @strengejacke, LGTM but I have a few comments

R/data_summary.R Outdated Show resolved Hide resolved
R/data_summary.R Outdated Show resolved Hide resolved
R/data_summary.R Outdated Show resolved Hide resolved
R/value_at.R Outdated Show resolved Hide resolved
R/data_summary.R Outdated Show resolved Hide resolved
@strengejacke strengejacke merged commit 7a1f372 into main Mar 4, 2024
25 of 27 checks passed
@strengejacke strengejacke deleted the data_summary branch March 4, 2024 15:32
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

Successfully merging this pull request may close these issues.

3 participants