Sort: Sort - Integer Sort - Sort a vector coresponding to another

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/Sort.R

Description

Fast sorting a vector.

Usage

1
2
3
Sort(x,descending=FALSE,partial=NULL,stable=FALSE,na.last=NULL)
Sort.int(x)
sort_cor_vectors(x, base, stable = FALSE, descending = FALSE)

Arguments

x

A numerical/integer/character vector with data.

base

A numerical/character vector to help sorting the x.

descending

A boolean value (TRUE/FALSE) for sorting the vector in descending order. By default sorts the vector in ascending.

partial

This argument has two usages. The first is an index number for sorting partial the vector. The second is a vector with 2 values, start and end c(start,end). Gives you a vector where the elements between start and end will be sorted only. Not character vector.

stable

A boolean value (TRUE/FALSE) for choosing a stable sort algorithm. Stable means that discriminates on the same elements. Not character vector.

na.last

Accept 4 values. TRUE, FALSE, NA, NULL.

TRUE/FALSE: for put NAs last or first.

NA: for remove NAs completely from vector.

NULL: by default. Leave it like that if there is no NA values.

Details

This function uses the sorting algorithm from C++. The implementation is very fast and highly optimised. Especially for large data.

Value

Sort and Sort.int: The sorted vector.

sort_cor_vectors: The first argument but sorted acording to the second.

Author(s)

Manos Papadakis

R implementation and documentation: Manos Papadakis <papadakm95@gmail.com>.

See Also

nth, colnth, rownth,sort_unique, Round

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
x <- rnorm(1000)
system.time( s1 <- Sort(x) )
system.time( s2 <- sort(x) )
all.equal(s1,s2) #true  but not if many duplicates.

system.time( s1 <- Sort(x,partial=100) )
system.time( s2 <- sort(x,partial=100) )
all.equal(s1,s2) #true


system.time( s1 <- Sort(x,stable=TRUE) )
system.time( s2 <- sort(x) )
all.equal(s1,s2) #true

x <- as.character(x)
system.time( s1 <- Sort(x) )
system.time( s2 <- sort(x) )
all.equal(s1,s2) #true

y <- runif(1000)
b <- sort_cor_vectors(x,y)

x<-rpois(100,100)
all.equal(Sort.int(x),sort.int(x))

x<-y<-y<-s1<-s2<-NULL

Example output

Loading required package: Rcpp
Loading required package: RcppZiggurat
   user  system elapsed 
  0.000   0.000   0.001 
   user  system elapsed 
  0.001   0.000   0.000 
[1] TRUE
   user  system elapsed 
  0.001   0.000   0.012 
   user  system elapsed 
  0.000   0.000   0.001 
[1] "Mean relative difference: 0.732686"
   user  system elapsed 
      0       0       0 
   user  system elapsed 
      0       0       0 
[1] TRUE
   user  system elapsed 
  0.002   0.000   0.002 
   user  system elapsed 
  0.005   0.000   0.006 
[1] TRUE
[1] TRUE

Rfast documentation built on Dec. 11, 2021, 9:59 a.m.