我想要删除特定条件下,按日期排序后客户的首个项目的score
时对应的所有client_id
的数据。据我所知,data.table
可以实现这一点。我已经接近实现。
以下是一些示例数据:
client_id <- c(1,1,1,2,2,3,3,3,3,4,4)
date <- c("1/1/2021", "1/2/2021", "1/3/2021", "5/1/2021", "10/1/2021", "10/1/2021", "11/1/2021", "1/2/2021", "10/9/2021", "15/9/2021", "16/10/2021")
date <- as.Date(date, '%d/%m/%Y')
score <- c(15,10,19,20,10,25,20,15,10,30,5)
df <- data.frame(client_id, date, score)
我尝试了这个:
df <-setDT(df)
df[client_id %in% df[score > 16, client_id], ]
我希望这将删除client_id 1,因为第一个分数<16。然而,似乎只有当所有分数>16时才会删除所有内容。