in_string_sort: Appropriately Sort Numerical Vectors with Prepended...

Description Usage Arguments Examples

View source: R/in_string_sort.R

Description

If a number is preceded by a character (as in L2), calling sort on these numbers will fall foul of the '10 before 2' issue. This script breaks the strings down into subsets of equal-length elements and then reorders. IMPORTANT to note is that this breaks down if the text surrounding the numbers differs in length.

Usage

1
2
in_string_sort(string, type = c("vector", "table"), table = NULL,
  cores = 1)

Arguments

string

The vector string (in the case of a vector) or table-like object column name or column index to be sorted.

type

One of 'vector' or 'table'. Entering anything else returns an error. If one enters 'table', the 'table' argument must also be satisfied.

table

Should be left blank if type = 'vector' or the data frame, matrix, tibble, etc. for sorting if type = 'table'.

cores

If the sorting is to be parallelised, enter the number of cores here. Defaults to one.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
unordered <- paste0('t', c(10, 100, 2, 20, 1))
sort(unordered)
[1] "t1"   "t10"  "t100" "t2"   "t20"
in_string_sort(string = unordered, type = 'vector')
[1] "t1"   "t2"   "t10"  "t20"  "t100"

unordered.mat <- matrix(data = c(1:20, unordered), ncol = 5)
unordered.mat
     [,1] [,2] [,3] [,4] [,5]
[1,] "1"  "6"  "11" "16" "t10"
[2,] "2"  "7"  "12" "17" "t100"
[3,] "3"  "8"  "13" "18" "t2"
[4,] "4"  "9"  "14" "19" "t20"
[5,] "5"  "10" "15" "20" "t1"

in_string_sort(string = 5, type = 'table', table = unordered.mat)
     [,1] [,2] [,3] [,4] [,5]
[1,] "5"  "10" "15" "20" "t1"
[2,] "3"  "8"  "13" "18" "t2"
[3,] "1"  "6"  "11" "16" "t10"
[4,] "4"  "9"  "14" "19" "t20"
[5,] "2"  "7"  "12" "17" "t100"

danjamesadams/Dantools documentation built on Aug. 24, 2019, 6:15 p.m.