If I have the values:
x <- 'random'
y <- 'word'
Can I do a test to see if x alphabetically comes before or after y? In this example something similar to a function that would produce:
alphabetical(x,y) -> True
alphabetical(y,x) -> False
The built-in comparison operators work fine on strings.
x < y
[1] TRUE
y < x
[1] FALSE
Note the details in the help page ?Comparison
, or perhaps more intuitively, ?`<`
, especially the importances of locale:
Comparison of strings in character vectors is lexicographic within the strings using the collating sequence of the locale in use [...]
Beware of making any assumptions about the collation order
alphabetical <- function(x,y){x < y}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With