Skip to content

Commit

Permalink
Merge pull request #38 from vbfelix/calc_perc
Browse files Browse the repository at this point in the history
closes #37
  • Loading branch information
vbfelix authored Jan 23, 2024
2 parents d4246e4 + 598e950 commit 1e9326e
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 108 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: relper
Title: Miscellaneous Functions for Data Preparation, Cleaning, and Visualization
Version: 3.10.1
Version: 3.11.2
Authors@R: person("Vinícius", "Félix", email = "vinicius1b7f@gmail.com", role = c("aut", "cre"))
Description: A collection of functions to help with data preparation, cleaning, and visualization. relper offers functions to: transform and format data (e.g., easily apply the percentage), compute metrics (e.g., coefficient of variation), check conditions (e.g., determine if a number is even or odd), supply lovely and varied color palettes (e.g., palettes inspired by TV series and movies), complement ggplot2 (e.g., add an identity line to a scatter plot), manipulate strings (e.g., keep only a type of character). The package's ultimate purpose is to simplify data analysis by providing functions that will shorten some steps.
Depends: R (>= 3.6.0)
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

export("%>%")
export(as_num)
export(as_perc)
export(calc_acf)
export(calc_association)
export(calc_auc)
Expand All @@ -15,6 +14,7 @@ export(calc_mean)
export(calc_modality)
export(calc_mode)
export(calc_peak_density)
export(calc_perc)
export(calc_skewness)
export(cut_by_quantile)
export(dttm_diff)
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
>
> *These additions transform the package into a comprehensive "Data Insight Toolbox," empowering users to gain deeper insights and perform advanced analyses on their datasets. Additionally, some functions have been renamed for better clarity and consistency.*
## relper 3.11.2

**Additions**

- calc_perc

**Removals**

- as_perc

## relper 3.10.1

**Additions**
Expand Down
39 changes: 0 additions & 39 deletions R/as_perc.r

This file was deleted.

49 changes: 49 additions & 0 deletions R/calc_perc.r
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)

}
32 changes: 0 additions & 32 deletions man/as_perc.Rd

This file was deleted.

32 changes: 32 additions & 0 deletions man/calc_perc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions tests/testthat/test-as_perc.R

This file was deleted.

56 changes: 56 additions & 0 deletions tests/testthat/test-calc_perc.R
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))

})

11 changes: 0 additions & 11 deletions vignettes/functions_as.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,6 @@ The goal of `as_num` is to be a more generalized version of `as.numeric`, where
as_num("123.456,78")
```

# as_perc

The goal of `as_perc` is to convert a number to a percentage. By default, the function simply multiplies values by 100.

```{r as_perc}
mtcars %>%
count(vs,am) %>%
mutate(prop = n/sum(n)) %>%
mutate(perc = as_perc(prop))
```

If you set the argument `sum` to `TRUE` the function will divide the values by their total and multiply by 100.

```{r as_perc-sum}
Expand Down
17 changes: 17 additions & 0 deletions vignettes/functions_calc.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,23 @@ pd_plot+
```

# calc_perc

The goal of `calc_perc` is to compute the percentage.

```{r calc_perc}
#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_skewness

The goal of `calc_skewness` is to compute a skewness coefficient.
Expand Down

0 comments on commit 1e9326e

Please sign in to comment.