-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from vbfelix/calc_perc
closes #37
- Loading branch information
Showing
11 changed files
with
166 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#' Compute a percentage | ||
#' | ||
#' @description Compute the percentage based on one or multiple variables. | ||
#' | ||
#' @eval arg_df("df") | ||
#' @param grp_var The grouping variable(s) based on which the percentages will be computed | ||
#' @param main_var An optional variable, when provided, will be utilized to compute the percentage for each of its own levels | ||
#' | ||
#' @return A data.frame summarizing the counts and percentages within groups. The output is arranged in descending order of counts within each group. | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' | ||
#' #without main_var | ||
#' calc_perc(mtcars,grp_var = c(cyl,vs)) | ||
#' | ||
#' #main_var within grp_var | ||
#' calc_perc(mtcars,grp_var = c(cyl,vs),main_var = vs) | ||
#' | ||
#' #main_var not within grp_var | ||
#' calc_perc(mtcars,grp_var = c(cyl),main_var = vs) | ||
|
||
calc_perc <- function(df,grp_var, main_var = NULL){ | ||
|
||
stopifnot(is.data.frame(df)) | ||
|
||
if(is.null(substitute(main_var))){ | ||
output <- | ||
df %>% | ||
dplyr::group_by(dplyr::across({{grp_var}})) %>% | ||
dplyr::summarise(n = dplyr::n(),.groups = 'drop') %>% | ||
dplyr::ungroup() %>% | ||
dplyr::mutate(perc = 100*n/sum(n,na.rm = TRUE)) %>% | ||
dplyr::arrange(-n) | ||
|
||
}else { | ||
output <- | ||
df %>% | ||
dplyr::group_by({{main_var}},across({{grp_var}})) %>% | ||
dplyr::summarise(n = dplyr::n(),.groups = 'drop') %>% | ||
dplyr::group_by({{main_var}}) %>% | ||
dplyr::mutate(perc = 100*n/sum(n,na.rm = TRUE)) %>% | ||
dplyr::arrange({{main_var}},-n) | ||
} | ||
|
||
return(output) | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
data_calc_perc <- | ||
data.frame( | ||
grp1 = c("a","b","a","b","c","c"), | ||
grp2 = c("1","2","1","2","1","2") | ||
) | ||
|
||
grp1 <- | ||
structure(list(grp1 = c("a", "b", "c"), | ||
n = c(2L, 2L, 2L), | ||
perc = c(33.3333333333333,33.3333333333333, 33.3333333333333)), | ||
row.names = c(NA, -3L), class = c("tbl_df","tbl", "data.frame")) | ||
|
||
grp2 <- | ||
structure(list(grp2 = c("1", "1", "2", "2"), | ||
grp1 = c("a", "c","b", "c"), | ||
n = c(2L, 1L, 2L, 1L), | ||
perc = c(66.6666666666667, 33.3333333333333, 66.6666666666667, 33.3333333333333)), | ||
class = c("grouped_df", "tbl_df", "tbl", "data.frame"), | ||
row.names = c(NA, -4L), | ||
groups = structure(list( | ||
grp2 = c("1", "2"), .rows = structure(list(1:2, 3:4), ptype = integer(0), | ||
class = c("vctrs_list_of","vctrs_vctr", "list"))), | ||
row.names = c(NA, -2L), .drop = TRUE, class = c("tbl_df","tbl", "data.frame"))) | ||
|
||
test_that("Expected equal", { | ||
|
||
expect_equal(object = calc_perc(mtcars,vs),expected = calc_perc(mtcars,"vs")) | ||
|
||
expect_equal( | ||
object = calc_perc(data_calc_perc,grp1), | ||
expected = grp1 | ||
) | ||
|
||
expect_equal( | ||
object = calc_perc(data_calc_perc,grp1,grp2), | ||
expected = grp2 | ||
) | ||
|
||
|
||
expect_equal( | ||
object = calc_perc(data_calc_perc,c(grp1,grp2),grp2), | ||
expected = grp2 | ||
) | ||
|
||
}) | ||
|
||
test_that("Wrong type", { | ||
|
||
expect_error(calc_perc(df = "a")) | ||
|
||
expect_error(calc_perc(df = 1)) | ||
|
||
expect_error(calc_perc(df = mtcars,grp_var = jojo)) | ||
|
||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters