简体   繁体   English

在R data.table中 - 按组按日期​​标记/更改列

[英]in R data.table - mark/change column based on date by group

I am trying to mark the maximum and minimum date of observations per ID using data.table. 我试图使用data.table标记每个ID的最大和最小观察日期。 Whilst I thought that this would be a straight forward exercise, I do not really understand why I do not obtain the result I wish: for some reason the following data.table command only flags the overall min and max and not "per ID", even though this is indicated: 虽然我认为这是一个简单的练习,但我真的不明白为什么我没有得到我希望的结果:由于某种原因,以下data.table命令只标记整体的最小值和最大值而不是“每个ID”,即使这表明:

Reproducible example (to mark maximum value by ID): 可重复的示例(通过ID标记最大值):

library(data.table)

date1 = as.POSIXct(Sys.Date(), "%m-%d-%Y-%X")
date2 = date1 - 70000
date3 = date1 - 7000
date4 = date1 + 90000

DT = data.table(ID= rep(1:2,each = 3), Date=c(date1,date2,date3,date4,date1,date2))

# create position marker (2 means middle value for date - not min/not max)
DT[,Position:=2]

# change position marker to 3 if latest date
DT[Date==max(Date),Position:=3, by=ID]

Why does data.table not consider the "by=ID" part? 为什么data.table不考虑“by = ID”部分? What am I overlooking? 我在俯瞰什么?

Version: Data.table 1.9.2 R: 3.0.3 版本:Data.table 1.9.2 R:3.0.3

I believe it is filtering the data, and the by statement follows. 我相信它正在过滤数据,并且by语句如下。 Perhaps what you want is: 也许你想要的是:

DT[, Position := ifelse(Date==max(Date),3,2), by= ID]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM