R data.table使用max in i语句 [英] R data.table using max in i statement

查看:149
本文介绍了R data.table使用max in i语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该这么简单,但由于某些原因data.table没有做我期望的。我想获取一行中最多两个值来确定是否应该过滤行。看起来发生的是,max()函数正在查看整个列,这不是我想要的。以下是代码:

This should be so simple but for some reason data.table is not doing what I expect. I want to take the max of two values in a row to determine if a row should be filtered or not. What appears to be happening is that the max() function is looking at the entire column which is not what I want. Here's the code:

> test_dt <- data.table(value1 = 1:10, value2 = 2:11, value3 = 3:12)
> test_dt[max(value1, value2, value3) < 7]
Empty data.table (0 rows) of 3 cols: value1,value2,value3

这是我的期望:

   value1 value2 value3
1:      1      2      3
2:      2      3      4
3:      3      4      5
4:      4      5      6

我在这里做错了吗?

推荐答案

您需要并行max或 pmax 。有关详情,请参阅?max

You want the parallel max, or pmax. See ?max for details:

test_dt[pmax(value1, value2, value3) < 7]
#    value1 value2 value3
# 1:      1      2      3
# 2:      2      3      4
# 3:      3      4      5
# 4:      4      5      6

如果你真的想要速度,可以使用 pmax.int ,再次参见?max

If you really want speed, you can use pmax.int, again, see ?max for details.

这篇关于R data.table使用max in i语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆