Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

For each ID return the earliest date from the start column and the latest date from the end column in r

I have a dataset which has multiple start dates and end dates for each Id. I would like to take the earliest date from the "startDate" column and the latest date from the endDate column.


data = data.frame(ID=c(1,1,1,1,2,2,2),
                  startDate= c("2018-01-31", "2018-01-31", "2018-01-31", "2019-06-06",
                          "2002-06-07", "2002-06-07", "2002-09-12"),
                  endDate = c(NA,NA,NA,"2019-07-09",NA,NA, "2002-10-02"))

This is the output I was hoping to get:

data = data.frame(ID=c(1,2),
                  startDate= c("2018-01-31","2002-06-07"),
                  endDate = c("2019-07-09","2002-10-02"))

After trying I have figured out how to do this through the following code, but would prefer something more efficient if at all possible. I am continuously needing to do this and i would rather not have to create two separate dataframes. Thank you guys for your help!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data_start <- data %>%
          group_by(ID) %>%
          arrange(startDate) %>%
             slice(1L)

data_end <- data %>%
  group_by(ID) %>%
  arrange(desc(endDate)) %>%
  slice(1L)

data <- left_join(data_start[,c(1,2)], data_end[,c(1,3)], by="ID")

>Solution :

Or with first and last:

library(dplyr)
data %>% 
  group_by(ID) %>%
  summarise(
    startDate = first(startDate),
    endDate = last(endDate)
  )
# A tibble: 2 x 3
     ID startDate  endDate   
* <dbl> <chr>      <chr>     
1     1 2018-01-31 2019-07-09
2     2 2002-06-07 2002-10-02
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading