cSort: Custom Sorting a Vector

Description Usage Arguments Value References Examples

View source: R/customSorting.R

Description

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Given a finite known number of exact words, perform a
custom sort of the words in vector x.  The parameter InitOrder
contains the exact literal words to be sorted and in the exact order.

This function is inspired by the custom sorting of database columns
that are done from inside the function dbWriteTable2
that is found inside the R CRAN package caroline.

R language S3 class implementations exist for some classes from packages
and R CRAN packages: base, zoo, chron, and timeDate.
Vector words are members of the following classes:
character, numeric(integer), logical,
POSIXct, POSIXlt, Date, yearmon, yearqtr, chron, times, dates, timeDate

Contributions are welcome.
1
2
3
4
5
6
7
8
About excess vector elements(not found in InitOrder) that exist in 'x'
They may be appended to the end. chopVectExc = F (default)
Choices are sort or not-sort. sortVectExc = T(default)
Choices are CI(case insensitive) sortVectExcCI = T sort (default)
or case sensitive sortVectExcCI = F sort

Excess Vector elements(in InitOrder)
that do not exist in 'x' are simply ignored.

Usage

 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
cSort(x, ...)

## Default S3 method:
cSort(x, ...)

## S3 method for class 'character'
cSort(x, InitOrder, sortVectExc = T, sortVectExcCI = F, chopVectExc = F, ...)

## S3 method for class 'numeric'
cSort(x, InitOrder, ...)

## S3 method for class 'logical'
cSort(x, InitOrder, ...)

## S3 method for class 'POSIXct'
cSort(x, InitOrder, ...)

## S3 method for class 'POSIXlt'
cSort(x, InitOrder, ...)

## S3 method for class 'Date'
cSort(x, InitOrder, ...)

## S3 method for class 'yearmon'
cSort(x, InitOrder, ...)

## S3 method for class 'yearqtr'
cSort(x, InitOrder, ...)

## S3 method for class 'chron'
cSort(x, InitOrder, ...)

## S3 method for class 'times'
cSort(x, InitOrder, ...)

## S3 method for class 'dates'
cSort(x, InitOrder, ...)

## S3 method for class 'timeDate'
cSort(x, InitOrder, ...)

Arguments

x

vector to be sorted

...

dots passed

InitOrder

starting custom sorting ( without the excess )

sortVectExc

sort vector excess. T(default) whether or not Vector excess words are attempted to be sorted (T) or not attempted to be sorted (F)

sortVectExcCI

F(default) whether or not Vector excess words that are not found in InitOrder are sorted 'not case insensitive'(T) or 'not case sensitive'(F). Sorting is done by "lower()"

chopVectExc

chop vector excess. F(default) whether or not excess (x) elements not found in InitOrder are removed

Value

vector sorted by InitOrder

References

Custom Sorting in R https://stackoverflow.com/questions/23995285/custom-sorting-in-r

Case insensitive sort of vector of string in R https://stackoverflow.com/questions/29890303/case-insensitive-sort-of-vector-of-string-in-r

David Schruth, (2013). caroline: A Collection of Database, Data Structure, Visualization, and Utility Functions for R. R package version 0.7.6. https://CRAN.R-project.org/package=caroline

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
## Not run: 
# word examples

# Know exact words: "date", "o", "h", "l", "c", "v", "a"
# that may mean: Date, Open, High, Low, Close, Volume, Average
# are "custom sorted".
# Excess words placed at the end
# and the excess words, "E2" and "e3", are
# sorted in case-insensitive alphabetical order.
#
cSort( c("a","v", "E2", "c","l", "e3" ,"h","o","date"),
   InitOrder = c("date", "o", "h", "l", "c", "v", "a"), sortVectExcCI = T
)
[1] "date" "o"    "h"    "l"    "c"    "v"    "a"    "E2"   "e3"


# excess, "B" and "b", is not sorted
cSort(c("B","b","A","a"), c("a","A"), sortVectExc = F)
[1] "a" "A" "B" "b"

# excess, "B" and "b", are sorted case senstive (default)
cSort(c("B","b","A","a"), c("a","A"))
[1] [1] "a" "A" "b" "B"

## End(Not run)
## Not run: 
# numeric examples

cSort(c(5, 2, 3, 4 ,1 ), c(4, 2, 3))
[1] 4 2 3 1 5

cSort(c(5, 2, 3.0001, 4 ,1 ), c(4, 2, 3.0001))
[1] 4.0000 2.0000 3.0001 1.0000 5.0000
class(.Last.value)
[1] "numeric"

# integer(numeric) example

cSort(c(5L, 2L, 3L, 4L ,1L ), c(4L, 2L, 3L))
[1] 4 2 3 1 5
class(.Last.value)
[1] "integer"

## End(Not run)
## Not run: 
# logical example

cSort(c(F ,T, F, T, T, F, T, F), F)
[1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE

## End(Not run)
## Not run: 
# Date examples

cSort(zoo::as.Date(6:3), zoo::as.Date(1:4))
[1] "1970-01-04" "1970-01-05" "1970-01-06" "1970-01-07"

cSort(zoo::as.Date(6:3), zoo::as.Date(1:4), sortVectExc = F)
[1] "1970-01-04" "1970-01-05" "1970-01-07" "1970-01-06"

cSort(zoo::as.Date(6:3), zoo::as.Date(1:4), sortVectExc = F, chopVectExc = T)
[1] "1970-01-04" "1970-01-05"

## End(Not run)

AndreMikulec/econModel documentation built on June 30, 2021, 9:48 a.m.