unique_: An alternative 'unique' function

View source: R/unique.R

unique_R Documentation

An alternative unique function

Description

unique_() is a usually faster alternative to unique() with optional sorting included. The internal API of this function is designed to be simple and generic to allow for working with all kinds of objects that can be reduced to a unique set.

Internally unique_() calculates unique group IDs for the given vector in the range ⁠[1, n]⁠ where 1 denotes the first group and n denotes the nth group. This function will work correctly as long as there is a correctly implemented collapse::GRP method and a [ method for the object. In the future cheapr will include a group_id S3 generic to replace the use of collapse::GRP here, of which is arguably more difficult to write correct methods for.

Usage

unique_(x, sort = FALSE)

Arguments

x

A vector (or data frame).

sort

Should unique result be sorted? Default is FALSE.

Value

A unique vector (or data frame).

Examples


library(cheapr)

x <- rep_(3:1, 3)
unique_(x)
unique_(x, sort = TRUE)

# Unique rows
iris |>
  sset(j = c("Petal.Width", "Species")) |>
  unique_()

cheapr documentation built on Nov. 28, 2025, 5:06 p.m.