I have the following data frame, I want to find the name of the column with the minimum date for each Id.
id a_date b_date c_date d_date
1 2014-01-01 2014-01-05 2014-01-15 NA
2 2017-02-01 NA NA 2015-03-01
Which is:
df <- tibble(id = c(1, 2),
a_date = c("01/01/14", "01/02/17"),
b_date = c("05/01/14", NA),
c_date = c("15/01/14", NA),
d_date = c(NA, "01/03/15"))
I tried this, but did not work
df %>% group_by(id) %>%
mutate( min = pmin(a_date, b_date, c_date, d_date ,na.rm=TRUE) %>%
mutate(col_name = which(colnames(df)== min)
Expected output will be as below
id col_name
1 a_date
2 d_date
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)