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

Sort

Sort - Integer Sort - Sort a vector coresponding to another


Description

Fast sorting a vector.

Usage

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

Examples

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

Rfast

A Collection of Efficient and Extremely Fast R Functions

v2.0.1
GPL (>= 2.0)
Authors
Manos Papadakis, Michail Tsagris, Marios Dimitriadis, Stefanos Fafalios, Ioannis Tsamardinos, Matteo Fasiolo, Giorgos Borboudakis, John Burkardt, Changliang Zou, Kleanthi Lakiotaki and Christina Chatzipantsiou.
Initial release
2020-09-13

We don't support your browser anymore

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