Felix T.
Felix T.

Reputation: 530

Finding cumulative maximum of Date objects

I'm trying to find the cumulative maximum Dates for a column in a data.frame. My data looks something like:

df <- data.frame(id = c(1, 2, 3, 4, 5, 7, 8, 11, 3, 12),
                 date = sample(seq(as.Date("2015-01-01"), as.Date("2017-01-01"), by = "day"), 10))

# > df
   id       date
1  15 2015-11-22
2   2 2016-06-23
3   4 2015-03-21
4   6 2015-04-09
5   5 2015-05-20
6   1 2016-01-03
7  13 2015-01-01
8  14 2016-11-24
9   7 2016-12-23
10  9 2016-08-12

Expected Result

I expect to end with something like:

# > df
   id       date
1  15 2015-11-22
2   2 2016-06-23
3   4 2016-06-23
4   6 2016-06-23
5   5 2016-06-23
6   1 2016-06-23
7  13 2016-06-23
8  14 2016-11-24
9   7 2016-12-23
10  9 2016-12-23

I have been successful finding cumulative maximums of numeric variables with dplyr::cummax() but this function isn't applicable to Date objects. Is there another function, maybe in base R, that would be analogous to cumsum() and can be used on dates?

Upvotes: 1

Views: 827

Answers (2)

IceCreamToucan
IceCreamToucan

Reputation: 28685

You can also use zoo::rollapply

df$cum_max_date <- zoo::rollapplyr(df$date, 1:nrow(df), max)

Results with set.seed(1) (same result as zack's)

   id       date cum_max_date
1   1 2015-07-14   2015-07-14
2   2 2015-09-30   2015-09-30
3   3 2016-02-23   2016-02-23
4   4 2016-10-24   2016-10-24
5   5 2015-05-27   2016-10-24
6   7 2016-10-15   2016-10-24
7   8 2016-11-16   2016-11-16
8  11 2016-04-24   2016-11-16
9   3 2016-03-31   2016-11-16
10 12 2015-02-14   2016-11-16

Upvotes: 2

zack
zack

Reputation: 5405

You could convert to numeric and then convert it back, noting that class Date has an origin of 1970-01-01:

set.seed(1)
df <- data.frame(id = c(1, 2, 3, 4, 5, 7, 8, 11, 3, 12),
                 date = sample(seq(as.Date("2015-01-01"), as.Date("2017-01-01"), by = "day"), 10))


df$cum_max_date <- as.Date(cummax(as.integer(df$date)), "1970-01-01")

df
#>    id       date cum_max_date
#> 1   1 2015-07-14   2015-07-14
#> 2   2 2015-09-30   2015-09-30
#> 3   3 2016-02-23   2016-02-23
#> 4   4 2016-10-24   2016-10-24
#> 5   5 2015-05-27   2016-10-24
#> 6   7 2016-10-15   2016-10-24
#> 7   8 2016-11-16   2016-11-16
#> 8  11 2016-04-24   2016-11-16
#> 9   3 2016-03-31   2016-11-16
#> 10 12 2015-02-14   2016-11-16

Created on 2019-05-21 by the reprex package (v0.2.1)

Upvotes: 2

Related Questions