Understanding .I in data.table in R

≡放荡痞女 提交于 2019-12-20 08:49:14

问题


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]


回答1:


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

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

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



来源:https://stackoverflow.com/questions/23585999/understanding-i-in-data-table-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!