理解 R 中 data.table 中的 .I [英] Understanding .I in data.table in R

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

问题描述

我在玩 data.table 时遇到了一个我不确定我是否完全理解的区别.给定以下数据集:

I was playing around with data.table and I came across a distinction that I'm not sure I quite understand. Given the following dataset:

library(data.table)

set.seed(400)
DT <- data.table(x = sample(LETTERS[1:5], 20, TRUE), key = "x"); DT

能否请您解释一下以下表达式之间的区别?

Can you please explain to me the difference between the following expressions?

1) DT[J("E"), .I]

2) DT[ , .I[x == "E"] ]

3) DT[x == "E", .I]

推荐答案

set.seed(400)
library(data.table)

DT <- data.table(x = sample(LETTERS[1:5], 20, TRUE), key = "x"); DT

1)

DT[  , .I[x == "E"] ] # [1] 18 19 20

是一个data.table,其中.I是一个向量,表示原始数据集DT

is a data.table where .I is a vector representing the row number of E in the ORIGINAL dataset DT

2)

DT[J("E")  , .I]   # [1] 1 2 3

DT["E"     , .I]   # [1] 1 2 3

DT[x == "E", .I]   # [1] 1 2 3

都是相同的,产生一个向量,其中 .Is 是表示新子集数据中 Es 的行号的向量

are all the same, producing a vector where .Is are vectors representing the row numbers of the Es in the NEW subsetted data

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

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