data.table 中的子集 [英] subsetting in data.table

查看:22
本文介绍了data.table 中的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对 data.table 进行子集化(来自包 data.table )在R(不是data.frame).我有一个 4 位数的年份作为密钥.我想通过一系列年份来细分.例如,我想拉取 1999、2000、2001 年的所有记录.

I am trying to subset a data.table ( from the package data.table ) in R (not a data.frame). I have a 4 digit year as a key. I would like to subset by taking a series of years. For example, I want to pull all the records that are from 1999, 2000, 2001.

我尝试在我的 DT[J(year)] 二进制搜索语法中传递以下内容:

I have tried passing in my DT[J(year)] binary search syntax the following:

1999,2000,2001
c(1999,2000,2001)
1999, 2000, 2001

但这些似乎都不起作用.任何人都知道如何做一个子集,其中您要选择的年份不仅仅是 1 年,而是多年?

but none of these seem to work. Anyone know how to do a subset where the years you want to select are not just 1 but multiple years?

推荐答案

适用于 data.frames 的适用于 data.tables.

What works for data.frames works for data.tables.

subset(DT, year %in% 1999:2001)

这篇关于data.table 中的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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