Conditionally Select Rows within a Group with Data.Table

╄→尐↘猪︶ㄣ 提交于 2021-01-27 12:23:21

问题


I am looking for solutions using data.table ― I have a data.table with the following columns:

data <- data.frame(GROUP=c(3,3,4,4,5,6),
                    YEAR=c(1979,1985,1999,2011,2012,1994),
                    NAME=c("S","A","J","L","G","A"))

data <- as.data.table(data)

Data.table:

GROUP  YEAR    NAME
3      1979    Smith 
3      1985    Anderson
4      1999    James
4      2011    Liam
5      2012    George
6      1994    Adams

For each group we want to select one row using the following rule:

  • If there is a year > 2000, select the row with minimum year above 2000.
  • If there not a year > 2000, select the row with the maximum year.

Desired output:

GROUP  YEAR    NAME
3      1985    Anderson
4      2011    Liam
5      2012    George
6      1994    Adams

Thanks! I have been struggling with this for a while.


回答1:


data.table should be a lot simpler if you subset the special .I row counter:

library(data.table)
setDT(data)
data[
  data[
        ,
        if(any(YEAR > 2000)) 
           .I[which.min(2000 - YEAR)] else
           .I[which.max(YEAR)],
        by=GROUP
      ]$V1
]
#   GROUP YEAR NAME
#1:     3 1985    A
#2:     4 2011    L
#3:     5 2012    G
#4:     6 1994    A

Thanks to @r2evans for the background info -

.I is an integer vector equivalent to seq_len(nrow(x)).
Ref: http://rdrr.io/cran/data.table/man/special-symbols.html

So, all I'm doing here is getting the matching row index for the whole of data for each of the calculations at each by= level. Then using these row indexes to subset data again.



来源:https://stackoverflow.com/questions/53255897/conditionally-select-rows-within-a-group-with-data-table

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