Data example.
date1 = seq(as.Date("2019/01/01"), by = "month", length.out = 29)
date2= seq(as.Date("2019/05/01"), by = "month", length.out = 29)
subproducts1=rep("1",29)
subproducts2=rep("2",29)
b1 <- c(rnorm(29,5))
b2 <- c(rnorm(29,5))
dfone <- data.frame("date"= c(date1,date2),
"subproduct"=
c(subproducts1,subproducts2),
"actuals"= c(b1,b2))
Max Date for Subproduct 1 is May 2021 and max date for Subproduct 2 is Sept 2021.
Question: Is there a way to:
The final result should be May 2021 in this case and able to handle multiple subproducts.
We may use slice_max
after grouping by 'subproduct', pull
the date
and get the min
, assign it to a new object
library(dplyr)
dfone %>%
group_by(subproduct) %>%
slice_max(n = 1, order_by = date) %>%
ungroup %>%
pull(date) %>%
min -> Min_date
-output
Min_date
[1] "2021-05-01"
Another option is to arrange
the rows and filter
using duplicated
dfone %>%
arrange(subproduct, desc(date)) %>%
filter(!duplicated(subproduct)) %>%
pull(date) %>%
min
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With