Choose groups to keep/drop in data.table

后端 未结 3 457
别跟我提以往
别跟我提以往 2020-12-21 06:41

How can I drop/keep groups according to a condition in data.table? Is there a better method than adding a new column, then filtering on that column and removing it?

相关标签:
3条回答
  • 2020-12-21 07:05

    You can use an if condition with .SD after grouping dt by column a:

    dt[, if(2 %in% b) .SD, a]
    
    #   a b
    #1: 1 5
    #2: 1 2
    #3: 1 2
    #4: 2 3
    #5: 2 5
    #6: 2 2
    

    From ?.SD, .SD is a data.table containing the Subset of x's Data for each group. Combined with if condition, we return nothing if 2 is not in column b and drop the corresponding group.

    0 讨论(0)
  • 2020-12-21 07:10

    I am sure that it is not the best solution, but that works.

    dt[a%in% dt[, a[2%in%b], by=a][, a],]
    
    0 讨论(0)
  • 2020-12-21 07:23

    Here is another method that uses .I to return the row indices of selected groups and then subsets on the row:

    dt[dt[, .I[2 %in% b], a]$V1]
       a b
    1: 1 5
    2: 1 2
    3: 1 2
    4: 2 3
    5: 2 5
    6: 2 2
    
    0 讨论(0)
提交回复
热议问题