Learn R Programming

hacksaw (version 0.0.2)

pluck_when: Pluck a value based on other criteria

Description

Pluck a value based on other criteria

Usage

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

Run this code
# NOT RUN {
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"))
# }

Run the code above in your browser using DataLab