我有以下data.frame:
forecast_date applicable_at forecasted_value
2008-01-01 2008-01-10 155
2008-01-04 2008-01-10 152
2008-01-06 2008-01-10 144
2008-01-02 2008-01-10 145
2008-01-03 2008-01-10 148
2008-01-05 2008-01-10 151
.............................................
data.frame将持续到2015-12-20
我想从这个data.frame中提取最后一个预测:
forecast_date applicable_at forecasted_value
2008-01-06 2008-01-10 144
我尝试使用data.table package
和setDT
功能,但我无法正确提取它。你能帮帮我吗?此外,我无法用子集函数解决这个问题。
我试过了:
newDF <- setDT(df)[, list(LastDate = max(forecast_date)), by = list(applicable_at)]
和
newDF <- subset(df, df$applicable_at == df$applicable_at & max(df$forecast_date))
答案 0 :(得分:1)
text1="forecast_date applicable_at forecasted_value
2008-01-01 2008-01-10 155
2008-01-04 2008-01-10 152
2008-01-06 2008-01-10 144
2008-01-02 2008-01-10 145
2008-01-03 2008-01-15 148
2008-01-05 2008-01-15 151"
dat <- read.table(text=text1, head=T, as.is=T)
使用dplyr
:
library(dplyr)
dat %>% group_by(applicable_at) %>% filter(forecast_date==max(forecast_date))
# Source: local data frame [2 x 3]
# Groups: applicable_at
# forecast_date applicable_at forecasted_value
# 1 2008-01-06 2008-01-10 144
# 2 2008-01-05 2008-01-15 151
使用data.table
:
library(data.table)
dt <- data.table(dat)
setkey(dt, applicable_at)
dt[, V1:=max(forecast_date), by=applicable_at][forecast_date == V1]
<强>更新强>
setDT(dat)[, V1:=max(forecast_date), by=applicable_at][forecast_date == V1]