Skip to content

Commit

Permalink
docs, test, examples, xref
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 4, 2024
1 parent 00be4ba commit 71c7b45
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
10 changes: 10 additions & 0 deletions R/data_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#' as a character string, e.g. `"mean_sepal_width = mean(Sepal.Width)"`. The
#' summary function `n()` can be used to count the number of observations.
#'
#' @seealso `value_at()`, which can be used inside a `data_summary()` call.
#'
#' @return A data frame with the requested summary statistics.
#'
#' @examples
Expand All @@ -42,6 +44,14 @@
#'
#' # count observations within groups
#' data_summary(mtcars, observations = n(), by = c("am", "gear"))
#'
#' # first and last observations of "mpg" within groups
#' data_summary(
#' mtcars,
#' first = value_at(mpg),
#' last = value_at(mpg, -1),
#' by = c("am", "gear")
#' )
#' @export
data_summary <- function(x, ...) {
UseMethod("data_summary")
Expand Down
8 changes: 5 additions & 3 deletions R/value_at.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' @title Find the value at a specific position in a variable
#' @title Find the value(s) at a specific position in a variable
#' @name value_at
#'
#' @description This function can be used to compute summary statistics for a
#' data frame or a matrix.
#' @description This function can be used to extract one or more values at a
#' specific position in a variable.
#'
#' @param x A vector or factor.
#' @param position An integer or a vector of integers, indicating the position(s)
Expand All @@ -13,6 +13,8 @@
#' computation.
#' @param default The value to be returned if the position is out of range.
#'
#' @seealso `data_summary()` to use `value_at()` inside a `data_summary()` call.
#'
#' @return A vector with the value(s) at the specified position(s).
#'
#' @examples
Expand Down
11 changes: 11 additions & 0 deletions man/data_summary.Rd

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

9 changes: 6 additions & 3 deletions man/value_at.Rd

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

3 changes: 3 additions & 0 deletions tests/testthat/test-value_at.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ test_that("value_at", {
expect_equal(value_at(efc$c12hour, 5:7), efc$c12hour[5:7], ignore_attr = TRUE)
expect_equal(value_at(efc$e42dep, 123456, default = 55), 55, ignore_attr = TRUE)
expect_null(value_at(efc$e42dep, 123456))
expect_null(value_at(efc$e42dep, NULL))
expect_error(value_at(efc$e42dep, NA), regex = "`position` can't")
expect_error(value_at(efc$e42dep, c(3, NA)), regex = "`position` can't")
})

0 comments on commit 71c7b45

Please sign in to comment.