Adamya Gaur
Adamya Gaur

Reputation: 17

How relational operators are behaving in R while comparing string with floats and integers?

var1=90
var2='mango'
var3=90.0
var1>var2
var2>var3

It gives :

var1>var2
#[1] FALSE

var2>var3
#[1] TRUE

Upvotes: 0

Views: 37

Answers (1)

Mohamed Desouky
Mohamed Desouky

Reputation: 4425

from the docs :

If the two arguments are atomic vectors of different types, one is coerced to the type of the other, the (decreasing) order of precedence being character, complex, numeric, integer, logical and raw.

so var1 and var3 coerced to character and Comparison of strings in character vectors is lexicographic

you can see that :

strtoi(charToRaw("9") , 16L)
[1] 57

for "9" and :

strtoi(charToRaw("m") , 16L)
[1] 109

for the character "m"

Upvotes: 1

Related Questions