Description Usage Arguments Value Examples
View source: R/matstat_utils.R
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.
1 | fastUnique(x, margin = 1L, freq = FALSE)
|
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 |
freq |
logical value if the frequency of the given rows or columns should also be returned (default: FALSE) |
fastUnique
returns a matrix without duplicated units. If
requested, the matrix has a freq
attribute containing the frequency
of the given rows or columns.
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))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.