max()函数输出错误

时间:2013-10-28 21:09:09

标签: r max

我有两个变量'y'和'dose',并尝试从这两个变量中确定最大值。但是,当我使用max(c(y,dose))时,它似乎只能识别一个变量。同样,当我使用min()时,它识别另一个变量。有谁知道为什么?

感谢。

> y
[1] "4.3" "4.1" "4.1" "4.1" "4.1" "4.3" "3.8" "4.5" "4"   "3.9" "4.3" "3.9"
[13] "4.2" "3.9" "4.3" "3.9" "4"   "4"   "4.1" "3.5" "3.8" "4"   "4.1" "3.9"
[25] "3.9" "4"   "4.1" "4.3" "3.7" "4.4"
> dose
[1] "16.03" "16.03" "22.9" 

> max(y)
[1] "4.5"
> max(dose)
[1] "22.9"
> max(c(y,dose))
[1] "4.5"

1 个答案:

答案 0 :(得分:1)

因为您正在比较字符串。

考虑...

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

因此,使用例如numeric

将变量转换为y <- as.numeric( y )类型