Learn R Programming

funprog (version 0.3.0)

unique_by: Unique with auxiliary function

Description

Remove duplicate elements, given a transformation.

Usage

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

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

Run the code above in your browser using DataLab