[Solved] max() function gives wrong output [closed]


Because you are comparing character strings.

Consider…

max("apple","banana","banana2")
#[1] "banana2"

max( "1" , "2" , "10" )
#[1] "2"

sort( c( "1" , "2" , "10" ) )
#[1] "1"  "10" "2" 

sort( as.integer( c("1" , "2" , "10" ) ) )
#[1]  1  2 10

max( as.integer( c("1" , "2" , "10" ) ) )
#[1] 10

So convert your variables to numeric types using for example y <- as.numeric( y )

2

solved max() function gives wrong output [closed]