Unique: Extract Unique Rows

Description Usage Arguments Value Note Author(s) Examples

Description

‘Unique’ returns a vector, data frame or array like 'x' but with duplicate elements removed.

Usage

1
Unique(X, rows.are.sets = FALSE)

Arguments

X

Numerical matrix.

rows.are.sets

If ‘TRUE’, rows are treated as sets - i.e., to define uniqueness, the order of the rows does not matter.

Value

Matrix of the same number of columns as x, with the unique rows in x sorted according to the columns of x. If rows.are.sets = TRUE the rows are also sorted.

Note

Unique’ is (under circumstances) much quicker than the more generic base function ‘unique’.

Author(s)

Raoul Grasman

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# `Unique' is faster than `unique'
x = matrix(sample(1:(4*8),4*8),ncol=4)
y = x[sample(1:nrow(x),3000,TRUE), ]
gc(); system.time(unique(y))
gc(); system.time(Unique(y))

#
z = Unique(y)
x[matorder(x),]
z[matorder(z),]

Example output

Loading required package: magic
Loading required package: abind
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 259657 13.9     460000 24.6   350000 18.7
Vcells 465943  3.6    1023718  7.9   786424  6.0
   user  system elapsed 
  0.011   0.000   0.012 
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 260620 14.0     592000 31.7   354808   19
Vcells 467854  3.6    1023718  7.9   786424    6
   user  system elapsed 
  0.002   0.000   0.006 
     [,1] [,2] [,3] [,4]
[1,]    7   25   12   19
[2,]    9    8   13   27
[3,]   17    5    1   26
[4,]   18   23   14    4
[5,]   22   28   15   21
[6,]   24   16   20   11
[7,]   29    3   31    2
[8,]   30   10    6   32
     [,1] [,2] [,3] [,4]
[1,]    7   25   12   19
[2,]    9    8   13   27
[3,]   17    5    1   26
[4,]   18   23   14    4
[5,]   22   28   15   21
[6,]   24   16   20   11
[7,]   29    3   31    2
[8,]   30   10    6   32

geometry documentation built on May 2, 2019, 6:09 p.m.

Related to Unique in geometry...