Understanding .I in data.table in R

后端 未结 1 825
梦毁少年i
梦毁少年i 2021-01-31 04: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(dat         


        
相关标签:
1条回答
  • 2021-01-31 04:27
    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

    0 讨论(0)
提交回复
热议问题