Remove a range in data.table

前端 未结 2 1863
灰色年华
灰色年华 2021-02-06 06:12

I am trying to exclude some rows from a datatable based on, let\'s say, days and month - excluding for example summer holidays, that always begin for example 15th of June and en

2条回答
  •  孤独总比滥情好
    2021-02-06 06:54

    Based on the answer here, you might try something like

    # Sample data
    DT <- data.table(Month = sample(c(1,3:12), 100, replace = TRUE),
      Day = sample(1:30, 100, replace = TRUE), key = "Month,Day")
    
    # Dates that you want to exclude
    excl <- as.data.table(rbind(expand.grid(6, 15:30), expand.grid(7, 1:15)))
    
    DT[-na.omit(DT[excl, which = TRUE])]
    

    If your data contain at least one entry for each day you want to exclude, na.omit might not be required.

提交回复
热议问题