HNSKD
HNSKD

Reputation: 1644

Using Max Function On Character Vectors in R

How do we understand max function on character vectors in R?

> max("MSP1","2C")
[1] "MSP1"
> max("202","6B")
[1] "6B"
> max("99","5C")
[1] "99"

When I read the documentation, it mentions that "Character versions are sorted lexicographically" and that "The type of the result will be that of the highest of the inputs in the hierarchy integer < double < character".

Does this mean that they compare the characters, position by position? In the first case, since "M" is greater than "2", then "MSP1" > "2C". Thus, only the first position from both characters are compared?

Upvotes: 0

Views: 2709

Answers (1)

Freeman
Freeman

Reputation: 6216

max comparison is case insensitive:

> max(c("a","Z"))
[1] "Z"
> max(c("A","z"))
[1] "z"

Upvotes: 1

Related Questions