Using the classic way to aggregate datasets x ~ id
by a function like sum
with a normal data.frame filter
it could be the following code.
Code
library(lubridate)
# Define your data
data <-
"id date x
1 2019-01-01 3
1 2018-12-01 4
1 2017-11-01 1
1 2017-10-01 2
1 2018-02-12 2
1 2017-09-01 2
"
# Read the table
tab <- read.csv(text=data, header = T, sep=' ')
# Find the youngest date
top.date <- as.Date(max(tab$date))
# Calculate the threshold (before and after point) of 6 month
thresh <- top.date) %m-% months(6)
# Calculate the sums over the ID's after the point date
after.thresh <- aggregate(x ~ id,
data = tab[as.Date(tab$date) >= thresh,],
FUN = sum)
# Calculate the sums over the ID's before the point date
before.thresh <- aggregate(x ~ id,
data = tab[as.Date(tab$date) < thresh,],
FUN=sum)
# Print the dates
cat("TOP.DATE.IS:", format_ISO8601(top.date),
" THRESH.DATE.IS:", format_ISO8601(thresh),"\n")
# Print the sums
cat("SUM.BEFORE.THRESH:", after.thresh$x,
"SUM.AFTER.THRESH:", before.thresh$x,"\n")
Results
TOP.DATE.IS: 2019-01-01 THRESH.DATE.IS: 2018-07-01
SUM.BEFORE.THRESH: 7 SUM.AFTER.THRESH: 7