具有不等式条件的R data.table连接 [英] R data.table join with inequality conditions

查看:121
本文介绍了具有不等式条件的R data.table连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用data.table包基于多个不平等条件对我的数据进行子集化. data.table手册中的示例显示了如何使用字符变量(而不是数字不等式)执行此操作.我也看到了如何使用子集函数来做到这一点.但是我真的很想利用data.table二进制搜索速度.以下是我要执行的操作的示例.

I would like to subset my data based on multiple inequality conditions using the data.table package. The examples in the data.table manual show how to do this with character variables, but not with numeric inequalities. I also see how to do this using the subset function. But I really would like to take advantage of the data.table binary search speed. Below is an example of what I am trying to do.

library(data.table)

data <- data.table(X=seq(-5,5,1), Y=seq(-5,5,1), Z=seq(-5,5,1))
data

setkey(data, X, Y, Z)

#the data.frame way
data[X > 0 & Y > 0 & Z > 0]

#the data.table way (does not work as I expected)
data[J(>0, >0, >0)]

推荐答案

使用dplyr软件包,该解决方案非常快速,简单.

The solution is quite fast and straightforward using the package dplyr.

install.packages(dplyr)
library(dplyr)

newdata <- filter(data, X > 0 , Y > 0 , Z > 0)

dplyr被证明是管理数据帧最简单,最快的软件包之一.在此处查看此出色的教程: http://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html

dplyr is showing to be one of the easiest and fastest packages for managing data frames. Check this great tutorial here: http://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html

RStudio团队还制作了一个不错的备忘单,位于: http://www.rstudio. com/resources/cheatsheets/

The RStudio team have alsoe produced a nice Cheat Sheet, here: http://www.rstudio.com/resources/cheatsheets/

这篇关于具有不等式条件的R data.table连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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