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

Group Index Does Not Print When Applying stby Through a Function on a List of Data Frames #192

Open
greg-botwin opened this issue Nov 12, 2023 · 1 comment

Comments

@greg-botwin
Copy link

Hi, I love the package. Huge thank you for your efforts in maintenance and improvements!

Issue: I am attempting to lapply through a large number of data frames and I am uncovering a minor annoyance. When calling stby through an apply function, the group index name fails to print. Any idea on how I can get the similar behavior as if I called 'stby' individually? Below is a minimal example.

library(summarytools)
test_list <- list(df1 = data.frame(Group = c("a", "b", "a"), Data = c(2,4,6)),
                  df2 = data.frame(Group = c("c", "d", "c"), Data = c(3,5, 7)))

test_function <- function(x){
  summary <- stby(data = x, 
                  INDICES   = x$Group, 
                  FUN       = dfSummary, 
                  stats     = "common", 
                  transpose = TRUE,
                  simplify = FALSE)
}

lapply(test_list, test_function)

Created on 2023-11-12 with reprex v2.0.2

Session info
sessionInfo()
#> R version 4.0.2 (2020-06-22)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.1252 
#> [2] LC_CTYPE=English_United States.1252   
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.29     withr_2.5.2       lifecycle_1.0.3   reprex_2.0.2     
#>  [5] evaluate_0.23     rlang_1.1.2       cli_3.6.1         rstudioapi_0.15.0
#>  [9] fs_1.5.2          rmarkdown_2.25    tools_4.0.2       glue_1.6.2       
#> [13] xfun_0.41         yaml_2.3.5        fastmap_1.1.0     compiler_4.0.2   
#> [17] htmltools_0.5.7   knitr_1.45
@dcomtois
Copy link
Owner

dcomtois commented Dec 5, 2024

Using dplyr::group_by() should do it:

test_list <- list(df1 = data.frame(Group = c("a", "b", "a"), Data = c(2,4,6)),
                  df2 = data.frame(Group = c("c", "d", "c"), Data = c(3,5, 7)))

test_function <- function(x){
  x |> group_by(Group) |> dfSummary()
}

(s <- lapply(test_list, test_function))

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

2 participants