评估R中的不等式

时间:2015-03-07 20:41:14

标签: r

我是R的新手,想评估以下声明:

DT17 >= DT13 >= DT37 > 0

这些是2D阵列。我对哪个陈述感到有点困惑。有没有办法得到2D的结果,其中0表示false,1表示true?

1 个答案:

答案 0 :(得分:1)

您可以使用&执行此操作。例如:

set.seed(123)
m1 <- matrix(sample(-3:3, 9, replace = TRUE), ncol = 3)
m2 <- matrix(sample(-3:3, 9, replace = TRUE), ncol = 3)
m3 <- matrix(sample(-3:3, 9, replace = TRUE), ncol = 3)

m1
     [,1] [,2] [,3]
[1,]   -1    3    0
[2,]    2    3    3
[3,]   -1   -3    0
m2
     [,1] [,2] [,3]
[1,]    0    1    3
[2,]    3    1   -2
[3,]    0   -3   -3
m3
     [,1] [,2] [,3]
[1,]   -1    1    1
[2,]    3    1    1
[3,]    3    3    0
res <- m1>m2 & m2 < m3 & m3 >= 0
      [,1]  [,2]  [,3]
[1,] FALSE FALSE FALSE
[2,] FALSE FALSE  TRUE
[3,] FALSE FALSE  TRUE

为了将其转换为数字:

res[] <- as.numeric(res)
res
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    1
[3,]    0    0    1