了解R中data.table中的.I [英] Understanding .I in data.table in R
本文介绍了了解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
是一个数据表,其中 .I
是一个向量,表示 E
中的行号原始数据集 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
都一样,产生一个向量,其中。I
s是代表 E $的行号的向量c $ c> s在新的子集数据中
are all the same, producing a vector where .I
s are vectors representing the row numbers of the E
s in the NEW subsetted data
这篇关于了解R中data.table中的.I的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文