Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Can you compare which value is first in alphabetical order?

Tags:

r

alphabetical

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

like image 285
Jason Melo Hall Avatar asked Dec 06 '22 15:12

Jason Melo Hall


2 Answers

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

like image 164
G5W Avatar answered Dec 08 '22 03:12

G5W


alphabetical <- function(x,y){x < y}
like image 45
sc73 Avatar answered Dec 08 '22 05:12

sc73