【发布时间】:2013-08-10 07:30:39
【问题描述】:
这是对 R 中returning the rows of a matrix that meet a condition 问题的扩展。假设我有矩阵:
one two three four
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 11 18
[4,] 4 9 11 19
[5,] 5 10 15 20
[6,] 1 6 15 20
[7,] 5 7 12 20
我想尽快返回所有行,其中matrix$two == 7 AND matrix$three == 12。这是我知道的方法:
out <- mat[mat$two == 7,]
final_out <- out[out$three == 12, ]
显然应该有一种方法可以在单行中获取final_out 的内容,例如:final_out <- which(mat$two == 7 && mat$three == 12),这比上面的两行代码更快更简洁。
返回这个多条件矩阵查询的最快 R 代码是什么?
【问题讨论】:
标签: r performance matrix conditional-statements multiple-columns