Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the minimum date between two maximum dates based off unique values in a column

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:

  1. Find the max date for both unique subproduct and
  2. Find the minimum date from the two max dates all in one step?

The final result should be May 2021 in this case and able to handle multiple subproducts.

like image 498
chriswang123456 Avatar asked Nov 30 '22 13:11

chriswang123456


1 Answers

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
like image 198
akrun Avatar answered Dec 04 '22 03:12

akrun