unique_by: Unique with auxiliary function

Description Usage Arguments Value Examples

View source: R/unique_by.R

Description

Remove duplicate elements, given a transformation.

Usage

1
unique_by(x, f, first = TRUE)

Arguments

x

a vector or a list.

f

a function to apply to each element of x. This function must produce comparable results.

first

if several elements are identical after being transformed by f, keep the first. Otherwise, keep the last.

Value

An object of the same type as x. Only elements that are unique after being transformed by f are kept.

Examples

1
2
3
4
unique_by(-3:2, abs)
unique_by(-3:2, abs, first = FALSE)
unique_by(c(1, 2, 4, 5, 6), function(x) x %% 3)
unique_by(list(1:2, 2:3, 2:4), length)

funprog documentation built on Jan. 13, 2021, 11:52 a.m.