pluck_when: Pluck a value based on other criteria

Description Usage Arguments Value Examples

View source: R/pluck_when.R

Description

Pluck a value based on other criteria

Usage

1
pluck_when(.x, .p, .i = 1, .else = NA)

Arguments

.x

Vector from which to select value.

.p

Logical expression.

.i

First TRUE index to return.

.else

If no matches from .p, value to return.

Value

A vector of length 1.

Examples

1
2
3
4
5
6
7
8
9
library(dplyr)
df <- tibble(
id = c(1, 1, 1, 2, 2, 2, 3, 3),
tested = c("no", "no", "yes", "no", "no", "no", "yes", "yes"),
year = c(2015:2017, 2010:2012, 2019:2020)
)
df %>%
 group_by(id) %>%
 mutate(year_first_tested = pluck_when(year, tested == "yes"))

hacksaw documentation built on Jan. 13, 2021, 7:57 a.m.