如何根据r中的条件(日期)更新data.table中的值

问题描述 投票:0回答:1

我想根据最近的日期更新dt中的值。

dt <- data.table(Name=c('John Smith', 'John Doe', 'Jeff Smith'),
             State=c('MI','WI','WI'), 
             Date = c("2018-1-2", "2018-1-4", "2018-1-6"),
             stringsAsFactors=F)
r data.table
1个回答
0
投票

这有效:

dt[State == "WI" & Date == dt[which.max(as.POSIXct(Date)), Date], Name := paste0(Name, "3")]
© www.soinside.com 2019 - 2024. All rights reserved.