Skip to content

Commit

Permalink
Concluded development work, added some unit tests and improved docume…
Browse files Browse the repository at this point in the history
…ntation and namespace management.
  • Loading branch information
LukasTang committed Sep 15, 2024
1 parent edc7f9f commit 0c87305
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 27 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: dplyr
Title: A Grammar of Data Manipulation
Version: 1.1.4.9000
Version: 1.1.4.9001
Authors@R: c(
person("Hadley", "Wickham", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-4757-117X")),
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ export(src_mysql)
export(src_postgres)
export(src_sqlite)
export(src_tbls)
export(standard_summary)
export(starts_with)
export(summarise)
export(summarise_)
Expand Down Expand Up @@ -492,6 +493,8 @@ importFrom(methods,setOldClass)
importFrom(pillar,glimpse)
importFrom(pillar,tbl_sum)
importFrom(pillar,type_sum)
importFrom(stats,quantile)
importFrom(stats,sd)
importFrom(stats,setNames)
importFrom(stats,update)
importFrom(tibble,add_row)
Expand Down
22 changes: 11 additions & 11 deletions R/standard_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#' 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 vars A character vector specifying the numeric 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")
#' groups = c("a","a","a","b","c"),
#' var1 = c(1, 2, 3, 4, 5),
#' var2 = c(6, 7, NA, 9, 10)
#' )
Expand All @@ -19,11 +19,7 @@
#' 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
#' @importFrom stats sd quantile
#'
#' @export
standard_summary <- function(df, vars, functions = list(
Expand All @@ -41,10 +37,14 @@ standard_summary <- function(df, vars, functions = list(
) {
gg <- as.character(groups(df))
summary_res <- df %>%
select(!!vars) %>%
select(all_of(vars)) %>%
summarise(across(.cols = c(!!vars), .fns = functions, .names = "{.col}xx_xx{.fn}")) %>%
ungroup() %>%
pivot_longer(cols = contains("xx")) %>%
pivot_longer(cols = contains("xx_xx")) %>%
mutate(value = as.numeric(value)) %>%
separate(name, into = c("VARIABLE", "STAT"), sep = "xx_xx") %>%
pivot_wider(id_cols = c(gg, "VARIABLE"), values_from = value, names_from = STAT)
}
pivot_wider(id_cols = c(gg, "VARIABLE"), values_from = value, names_from = STAT) %>%
as.data.frame()
return(summary_res)

}
41 changes: 41 additions & 0 deletions man/standard_summary.Rd

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

35 changes: 20 additions & 15 deletions tests/testthat/test-standard-summary.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
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)
groups = c("a","a","a","b","c"),
var1 = c(1, 2, 3, 4, 5),
var2 = c(6, 7, NA, 9, 10)
)

# Call the standard summary function
result <- standard_summary(df)


# Calculate standard summary statistics for var1 and var2
result <- starwars %>%
filter(!is.na(sex)) %>%
group_by(sex) %>%
standard_summary(vars=c("height", "mass","birth_year"))

# 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)
expect_true(is.data.frame(result))

# Check if the result has the correct number of rows and columns
expect_equal(nrow(result), 12)
expect_equal(df_n_col(result), 13)

# Check if the result contains the correct summary statistics
expect_equal(result[1, "x"], 3)
expect_equal(result[2, "y"], 8)
})
expect_equal(result[1,"min"], 150)
expect_equal(result[2, "med"] , 55)
expect_equal(result[7,"nmiss"],3)
})
Empty file.

0 comments on commit 0c87305

Please sign in to comment.