Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

bit_sort_unique

bit sort unique


Description

fast combination of sort and unique for integers

Usage

bit_sort_unique(
  x,
  decreasing = FALSE,
  na.last = NA,
  has.dup = TRUE,
  range_na = NULL
)

Arguments

x

an integer vector

decreasing

FALSE (ascending) or TRUE (descending)

na.last

NA removes NAs, FALSE puts NAs at the beginning, TRUE puts NAs at the end

has.dup

TRUE (the default) assumes that x might have duplicates, set to FALSE if duplicates are impossible

range_na

NULL calls range_na, optionally the result of range_na can be given here to avoid calling it again

Details

determines the range of the integers and checks if the density justifies use of a bit vector; if yes, creates the result using a bit vector; if no, falls back to sort(unique())

Value

a sorted unique integer vector

See Also

Examples

bit_sort_unique(c(2L,1L,NA,NA,1L,2L))
bit_sort_unique(c(2L,1L,NA,NA,1L,2L), na.last=FALSE)
bit_sort_unique(c(2L,1L,NA,NA,1L,2L), na.last=TRUE)
bit_sort_unique(c(2L,1L,NA,NA,1L,2L), decreasing = TRUE)
bit_sort_unique(c(2L,1L,NA,NA,1L,2L), decreasing = TRUE, na.last=FALSE)
bit_sort_unique(c(2L,1L,NA,NA,1L,2L), decreasing = TRUE, na.last=TRUE)

## Not run: 
x <- sample(1e7, replace=TRUE)
system.time(bit_sort_unique(x))
system.time(sort(unique(x)))
x <- sample(1e7)
system.time(bit_sort_unique(x))
system.time(sort(x))

## End(Not run)

bit

Classes and Methods for Fast Memory-Efficient Boolean Selections

v4.0.4
GPL-2 | GPL-3
Authors
Jens Oehlschlägel [aut, cre], Brian Ripley [ctb]
Initial release
2020-08-03

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.