-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Draft new
data_summary()
function (#482)
* Draft new `data_summary()` function * check if we can avoid duplicated code * pkgdown * fix * fixes * lintr * add tests * code style * desc, news * fix * add print method and snapshot test * add test * correct english form * test * include NA, sort output * add test * meaningful code comments * add test * allow n() * docs * only n() * informative error for incorrect length, add "value_at()" function * pkgdown * docs, test, examples, xref * add test * fix tests * fix * lintr * lintr * lintr * lintr (reduce complexity) * fix * fix * address comments * use text_concatenate * remove value_at, move code into dev-folder * update docs * update test
- Loading branch information
1 parent
83f9703
commit 7a1f372
Showing
15 changed files
with
805 additions
and
138 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
test_that("value_at", { | ||
data(efc, package = "datawizard") | ||
expect_equal(value_at(efc$e42dep, 5), 4, ignore_attr = TRUE) | ||
expect_equal(value_at(efc$c12hour, 4), NA_real_, ignore_attr = TRUE) | ||
expect_equal(value_at(efc$c12hour, 4, remove_na = TRUE), 168, ignore_attr = TRUE) | ||
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") | ||
}) |
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,52 @@ | ||
#' @title Find the value(s) at a specific position in a variable | ||
#' @name value_at | ||
#' | ||
#' @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) | ||
#' of the value(s) to be returned. Negative values are counted from the end of | ||
#' the vector. If `NA`, an error is thrown. | ||
#' @param remove_na Logical, if `TRUE`, missing values are removed before | ||
#' computing the position. If `FALSE`, missing values are included in the | ||
#' 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 | ||
#' data(mtcars) | ||
#' # 5th value | ||
#' value_at(mtcars$mpg, 5) | ||
#' # last value | ||
#' value_at(mtcars$mpg, -1) | ||
#' # out of range, return default | ||
#' value_at(mtcars$mpg, 150) | ||
#' # return 2nd and fifth value | ||
#' value_at(mtcars$mpg, c(2, 5)) | ||
#' @export | ||
value_at <- function(x, position = 1, default = NULL, remove_na = FALSE) { | ||
if (remove_na) { | ||
x <- x[!is.na(x)] | ||
} | ||
n <- length(x) | ||
unlist(lapply(position, .values_at, x = x, n = n, default = default), use.names = FALSE) | ||
} | ||
|
||
# helper ---- | ||
|
||
.values_at <- function(x, position, n, default) { | ||
if (is.na(position)) { | ||
insight::format_error("`position` can't be `NA`.") | ||
} | ||
if (position < 0L) { | ||
position <- position + n + 1 | ||
} | ||
if (position <= 0 || position > n) { | ||
return(default) | ||
} | ||
x[position] | ||
} |
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,7 +1,7 @@ | ||
Type: Package | ||
Package: datawizard | ||
Title: Easy Data Wrangling and Statistical Transformations | ||
Version: 0.9.1.4 | ||
Version: 0.9.1.5 | ||
Authors@R: c( | ||
person("Indrajeet", "Patil", , "[email protected]", role = "aut", | ||
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")), | ||
|
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
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
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
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
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
Oops, something went wrong.