diff --git a/R/data_summary.R b/R/data_summary.R index b8f6e0ed3..bddd360ca 100644 --- a/R/data_summary.R +++ b/R/data_summary.R @@ -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 @@ -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") diff --git a/R/value_at.R b/R/value_at.R index 2b923b6ff..cdadc9dc6 100644 --- a/R/value_at.R +++ b/R/value_at.R @@ -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) @@ -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 diff --git a/man/data_summary.Rd b/man/data_summary.Rd index 0db624338..ba3e6cd46 100644 --- a/man/data_summary.Rd +++ b/man/data_summary.Rd @@ -56,4 +56,15 @@ data_summary(mtcars, "MW = mean(mpg)", "SD = sd(mpg)", by = c("am", "gear")) # 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") +) +} +\seealso{ +\code{value_at()}, which can be used inside a \code{data_summary()} call. } diff --git a/man/value_at.Rd b/man/value_at.Rd index a2b1058ca..2d6c05985 100644 --- a/man/value_at.Rd +++ b/man/value_at.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/value_at.R \name{value_at} \alias{value_at} -\title{Find the value at a specific position in a variable} +\title{Find the value(s) at a specific position in a variable} \usage{ value_at(x, position = 1, default = NULL, remove_na = FALSE) } @@ -23,8 +23,8 @@ computation.} A vector with the value(s) at the specified position(s). } \description{ -This function can be used to compute summary statistics for a -data frame or a matrix. +This function can be used to extract one or more values at a +specific position in a variable. } \examples{ data(mtcars) @@ -37,3 +37,6 @@ value_at(mtcars$mpg, 150) # return 2nd and fifth value value_at(mtcars$mpg, c(2, 5)) } +\seealso{ +\code{data_summary()} to use \code{value_at()} inside a \code{data_summary()} call. +} diff --git a/tests/testthat/test-value_at.R b/tests/testthat/test-value_at.R index a15256004..b6cc60a0d 100644 --- a/tests/testthat/test-value_at.R +++ b/tests/testthat/test-value_at.R @@ -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") })