user10853
user10853

Reputation: 302

Comparing (alphanumeric) character strings in R

Consider the following:

"16D" < "7A"

returns TRUE.

Why is that and how can I compare such character strings such that the number is compared first then the letter? This way the answer would be false because 16>7 and D>A?

Upvotes: 4

Views: 256

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226362

Can you adapt this?

library("gtools")
(m <- mixedorder(c("16D","7A")))
## [1] 2 1
m[1] < m[2] ## FALSE

Upvotes: 5

Related Questions