first_present: Extract first or last non-NA value from a vector

View source: R/first_present.R

first_presentR Documentation

Extract first or last non-NA value from a vector

Description

Returns the first or last non-NA value of x when sorted by order_by.

Usage

first_present(x, order_by = NULL, default = NA)

last_present(x, order_by = NULL, default = NA)

Arguments

x

A vector

order_by

Another vector to sort x by.

default

A default value to use if the position does not exist in the input.

Value

The first or last non-NA value in x.

Note

If there are no non-NA values, NA is returned.

Examples

first_present(x = c(NA, 1, 2, NA), order_by = c(4, 3, 2, 1))
last_present(x = c(NA, 1, 2, NA), order_by = c(4, 3, 2, 1))

first_present(x = c(NA, 1, 2, NA), order_by = c(1, 2, 3, 4))
last_present(x = c(NA, 1, 2, NA), order_by = c(1, 2, 3, 4))

CCMH-PSU/CCMHr documentation built on Jan. 31, 2024, 1:08 a.m.