Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add na_rm argument to nth(), first(), and last() #6257

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

# dplyr 1.0.9



* `nth()`, `first()`, and `last()` gained the `na_rm` argument since they are
summary functions (#6242).

* New `rows_append()` which works like `rows_insert()` but ignores keys and
allows you to insert arbitrary rows with a guarantee that the type of `x`
won't change (#6249, thanks to @krlmlr for the implementation and @mgirlich
Expand Down
15 changes: 10 additions & 5 deletions R/nth-value.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#'
#' For more complicated objects, you'll need to supply this value.
#' Make sure it is the same type as `x`.
#' @param na_rm if `TRUE` missing values don't count
#' @return A single value. `[[` is used to do the subsetting.
#' @export
#' @examples
Expand All @@ -39,12 +40,16 @@
#'
#' # These functions always return a single value
#' first(integer())
nth <- function(x, n, order_by = NULL, default = default_missing(x)) {
nth <- function(x, n, order_by = NULL, default = default_missing(x), na_rm = FALSE) {
if (length(n) != 1 || !is.numeric(n)) {
abort("`n` must be a single integer.")
}
n <- trunc(n)

if (na_rm) {
x <- x[!is.na(x)]
}

if (n == 0 || n > length(x) || n < -length(x)) {
return(default)
}
Expand All @@ -63,14 +68,14 @@ nth <- function(x, n, order_by = NULL, default = default_missing(x)) {

#' @export
#' @rdname nth
first <- function(x, order_by = NULL, default = default_missing(x)) {
nth(x, 1L, order_by = order_by, default = default)
first <- function(x, order_by = NULL, default = default_missing(x), na_rm = FALSE) {
nth(x, 1L, order_by = order_by, default = default, na_rm = na_rm)
}

#' @export
#' @rdname nth
last <- function(x, order_by = NULL, default = default_missing(x)) {
nth(x, -1L, order_by = order_by, default = default)
last <- function(x, order_by = NULL, default = default_missing(x), na_rm = FALSE) {
nth(x, -1L, order_by = order_by, default = default, na_rm = na_rm)
}

default_missing <- function(x) {
Expand Down
8 changes: 5 additions & 3 deletions man/nth.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-nth-value.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ test_that("nth() gives meaningful error message (#5466)", {
(expect_error(nth(1:10, "x")))
})
})

test_that("nth() works with NA values when na_rm = TRUE", {
expect_equal(nth(c(as.numeric(NA), 1:3), 1, na_rm = TRUE), 1)
expect_equal(nth(c(1, as.numeric(NA), 2, 3), 2, na_rm = TRUE), 2)
expect_equal(nth(c(1:3, as.numeric(NA)), -1, na_rm = TRUE), 3)
})

test_that("nth() uses default value when na_rm = TRUE and all NA values", {
expect_equal(nth(rep(as.numeric(NA), 3), 1, na_rm = TRUE), as.numeric(NA))
expect_equal(nth(rep(as.numeric(NA), 3), 2, na_rm = TRUE), as.numeric(NA))
expect_equal(nth(rep(as.numeric(NA), 3), -1, na_rm = TRUE), as.numeric(NA))
})