写两个值之间的最大值/最小值

use*_*097 0 r

我有一个双列矩阵,我想生成一个新的矩阵/ data.frame,其中如果最大则Col N为1,否则为0(它们永远不相等).这是我的尝试:

testM <- matrix(c(1,2,3, 1,1,5), ncol = 2, byrow = T)
>testM
    V1  V2
1   1   2
2   3   1
3   1   5

apply(data.frame(testM), 1, function(row) ifelse(max(row[1],row[2]),1,0))
Run Code Online (Sandbox Code Playgroud)

我希望有:

0 1
1 0
0 1
Run Code Online (Sandbox Code Playgroud)

因为max()函数中的0,1参数,但我得到了

[1] 1 1 1
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Bat*_*hek 5

或使用 pmax

testM <- matrix(c(1,2,3, 1,1,5), ncol = 2, byrow = T)

--(testM==pmax(testM[,1],testM[,2]))

     V1 V2
[1,]  0  1
[2,]  1  0
[3,]  0  1
Run Code Online (Sandbox Code Playgroud)