-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,50 @@ | ||
standard_summary <- function(df | ||
, vars | ||
, functions = list( | ||
mean = ~mean(.x,na.rm=T) | ||
, sd = ~sd(.x,na.rm=T) | ||
, min = ~min(.x,na.rm=T) | ||
, q10 = ~quantile(.x,0.1,na.rm=T) | ||
, q25 = ~quantile(.x,0.25,na.rm=T) | ||
, med = ~quantile(.x,0.5,na.rm=T) | ||
, q75 = ~quantile(.x,0.75,na.rm=T) | ||
, q90 = ~quantile(.x,0.90,na.rm=T) | ||
, max = ~max(.x,na.rm=T) | ||
, n = ~n() | ||
, nmiss = ~length(which(is.na(.x)) | ||
){ | ||
#' Standard Summary | ||
#' | ||
#' This function calculates standard summary statistics for specified variables in a data frame. | ||
#' | ||
#' @param df A data frame. | ||
#' @param vars A character vector specifying the variables for which summary statistics should be calculated. | ||
#' @param functions A list of functions to be applied to each variable. The default functions include mean, standard deviation, minimum, 10th percentile, 25th percentile, median, 75th percentile, 90th percentile, maximum, count, and count of missing values. | ||
#' | ||
#' @return A data frame containing the calculated summary statistics. | ||
#' | ||
#' @examples | ||
#' df <- data.frame( | ||
#' groups = c("a","a","a","b","c") | ||
#' var1 = c(1, 2, 3, 4, 5), | ||
#' var2 = c(6, 7, NA, 9, 10) | ||
#' ) | ||
#' | ||
#' # Calculate standard summary statistics for var1 and var2 | ||
#' summary <- df %>% group_by(groups) %>% standard_summary(c("var1", "var2")) | ||
#' print(summary) | ||
#' | ||
#' @import dplyr | ||
#' @import tidyr | ||
#' @import stringr | ||
#' @importFrom stats mean sd min quantile max | ||
#' @importFrom base length which is.na | ||
#' | ||
#' @export | ||
standard_summary <- function(df, vars, functions = list( | ||
mean = ~mean(.x, na.rm = TRUE), | ||
sd = ~sd(.x, na.rm = TRUE), | ||
min = ~min(.x, na.rm = TRUE), | ||
q10 = ~quantile(.x, 0.1, na.rm = TRUE), | ||
q25 = ~quantile(.x, 0.25, na.rm = TRUE), | ||
med = ~quantile(.x, 0.5, na.rm = TRUE), | ||
q75 = ~quantile(.x, 0.75, na.rm = TRUE), | ||
q90 = ~quantile(.x, 0.90, na.rm = TRUE), | ||
max = ~max(.x, na.rm = TRUE), | ||
n = ~n(), | ||
nmiss = ~length(which(is.na(.x)))) | ||
) { | ||
gg <- as.character(groups(df)) | ||
summary_res <- df %>% | ||
select(!!vars) %>% | ||
summarise(across(.cols=c(!!vars),.fns = functions, .names="{.col}xx_xx{.fn}")) %>% | ||
ungroup() %>% | ||
pivot_longer(cols=contains("xx")) %>% | ||
separate(name, into = c("VARIABLE", "STAT"),sep="xx_xx") %>% | ||
pivot_wider(id_cols = c(gg, "VARIABLE"), values_from = value, names_from = STAT) | ||
} | ||
summary_res <- df %>% | ||
select(!!vars) %>% | ||
summarise(across(.cols = c(!!vars), .fns = functions, .names = "{.col}xx_xx{.fn}")) %>% | ||
ungroup() %>% | ||
pivot_longer(cols = contains("xx")) %>% | ||
separate(name, into = c("VARIABLE", "STAT"), sep = "xx_xx") %>% | ||
pivot_wider(id_cols = c(gg, "VARIABLE"), values_from = value, names_from = STAT) | ||
} |
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,21 @@ | ||
test_that("standard summary returns correct results", { | ||
# Create a test data frame | ||
df <- data.frame( | ||
x = c(1, 2, 3, 4, 5), | ||
y = c(6, 7, 8, 9, 10) | ||
) | ||
|
||
# Call the standard summary function | ||
result <- standard_summary(df) | ||
|
||
# Check if the result is a data frame | ||
expect_is(result, "data.frame") | ||
|
||
# Check if the result has the correct number of rows and columns | ||
expect_equal(nrow(result), 2) | ||
expect_equal(ncol(result), 2) | ||
|
||
# Check if the result contains the correct summary statistics | ||
expect_equal(result[1, "x"], 3) | ||
expect_equal(result[2, "y"], 8) | ||
}) |