fastUnique: Fast version of unique for matrices

Description Usage Arguments Value Examples

View source: R/matstat_utils.R

Description

fastUnique finds the unique rows or columns of a matrix. It is much faster and also more memory-efficient than unique.matrix, especially if the input matrix is not a character matrix.

Usage

1
fastUnique(x, margin = 1L, freq = FALSE)

Arguments

x

matrix

margin

1 (default) or 2, referring to rows and columns of the matrix, respectively. Can be a character string which is interpreted as the name of the dimension if x has named dimnames.

freq

logical value if the frequency of the given rows or columns should also be returned (default: FALSE)

Value

fastUnique returns a matrix without duplicated units. If requested, the matrix has a freq attribute containing the frequency of the given rows or columns.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# create a matrix of random integer values ranging from 1 to 3
x <- matrix_(sample(1:3, 4e5, TRUE), 1e5, 4)

# compare computation times
system.time(un_x <- unique(x))
system.time(un_x_fast <- fastUnique(x))

# the same if x has dimension names
decorateDims_(x)
system.time(un_x <- unique(x))
system.time(un_x_fast <- fastUnique(x))

# the results are identical
stopifnot(identical(un_x, un_x_fast))

# fastUnique can also return frequency counts
system.time(un_x_withfreq <- fastUnique(x, freq = TRUE))

# check that frequencies add up to the number of rows of x
stopifnot(sum(attr(un_x_withfreq, "freq")) == nrow(x))

tdeenes/eegR documentation built on April 19, 2021, 4:17 p.m.