mixedsort.html
3.01 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>R: Order or Sort strings with embedded numbers so that the numbers
are in the correct order</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../../R.css">
</head><body>
<table width="100%" summary="page for mixedsort {gtools}"><tr><td>mixedsort {gtools}</td><td align="right">R Documentation</td></tr></table>
<h2>Order or Sort strings with embedded numbers so that the numbers
are in the correct order</h2>
<h3>Description</h3>
<p>
These functions sort or order character strings containing numbers so
that the numbers are numerically sorted rather than sorted by
character value. I.e. "Asprin 50mg" will come before "Asprin 100mg".
In addition, case of character strings is ignored so that "a", will
come before "B" and "C".
</p>
<h3>Usage</h3>
<pre>
mixedsort(x)
</pre>
<h3>Arguments</h3>
<table summary="R argblock">
<tr valign="top"><td><code>x</code></td>
<td>
Character vector to be sorted </td></tr>
</table>
<h3>Details</h3>
<p>
I often have character vectors (e.g. factor labels) that contain
both text and numeric data, such as compound and dose. This function
is useful for sorting these character vectors into a logical order.
</p>
<p>
It does so by splitting each character vector into a sequence of
character and numeric sections, and then sorting along these sections,
with numbers being sorted by numeric value (e.g. "50" comes before
"100"), followed by characters strings sorted by character
value (e.g. "A" comes before "B").
</p>
<p>
Empty strings are always sorted to the front of the list, and <code>NA</code>
values to the end.
</p>
<h3>Value</h3>
<p>
<code>mixedorder</code> returns a vector giving the sort order of the input
elements. <code>mixedsort</code> returns the sorted vector.</p>
<h3>Author(s)</h3>
<p>
Gregory R. Warnes <a href="mailto:warnes@bst.rochester.edu">warnes@bst.rochester.edu</a>
</p>
<h3>See Also</h3>
<p>
<code><a href="../../base/html/sort.html">sort</a></code>, <code><a href="../../base/html/order.html">order</a></code>
</p>
<h3>Examples</h3>
<pre>
# compound & dose labels
Treatment <- c("Control", "Asprin 10mg/day", "Asprin 50mg/day",
"Asprin 100mg/day", "Acetomycin 100mg/day",
"Acetomycin 1000mg/day")
# ordinary sort puts the dosages in the wrong order
sort(Treatment)
# but mixedsort does the 'right' thing
mixedsort(Treatment)
# Here is a more complex example
x <- rev(c("AA 0.50 ml", "AA 1.5 ml", "AA 500 ml", "AA 1500 ml",
"EXP 1", "AA 1e3 ml", "A A A", "1 2 3 A", "NA", NA, "1e2",
"", "-", "1A", "1 A", "100", "100A", "Inf"))
mixedorder(x)
mixedsort(x)
# notice that plain numbers, including 'Inf' show up before strings.
</pre>
<hr><div align="center">[Package <em>gtools</em> version 2.4.0 <a href="00Index.html">Index]</a></div>
</body></html>