Split a column with dates to two columns end and start date

Dear Community,
I would like to split a column with dates to two columns with start date and end date.

I will list some dates below, so as to provide some example data.

 [1] "2022-03-11 UTC" "2022-03-16 UTC" "2022-03-21 UTC" "2022-03-26 UTC" "2022-04-05 UTC" "2022-04-10 UTC" "2022-04-15 UTC" "2022-04-25 UTC"
 [9] "2022-04-30 UTC" "2022-05-10 UTC" "2022-05-15 UTC" "2022-05-20 UTC" "2022-05-25 UTC"

So for this example i would like to create a dataframe where the first row is compesed by:

  1. Start_date = "2022-03-11 UTC"
  2. End_date = "2022-03-16 UTC"

Thanks

What would be the criteria to define which date is the start and which is the end? you have 13 elements so you can't have 7 pairs of dates.

Perhaps I have explained myself badly, I will try to make you understand my need with the following pictures.

Considering the following list of dates

I would like to know if there is a function that can split these dates into two columns with start_date and end_date as in the following figure

Thanks

To address this, next time please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Now about your question, does this solve your problem?

library(dplyr)

# Sample date vector
as_date <- c("2022-03-11 UTC", "2022-03-16 UTC", "2022-03-21 UTC",
             "2022-03-26 UTC", "2022-04-05 UTC", "2022-04-10 UTC",
             "2022-04-15 UTC", "2022-04-25 UTC",  "2022-04-30 UTC",
             "2022-05-10 UTC", "2022-05-15 UTC", "2022-05-20 UTC",
             "2022-05-25 UTC")

# Relevant code
as.data.frame(as_date) %>% 
    transmute(start_date = as_date,
              end_date = lead(start_date)) %>% 
    head(-1)
#>        start_date       end_date
#> 1  2022-03-11 UTC 2022-03-16 UTC
#> 2  2022-03-16 UTC 2022-03-21 UTC
#> 3  2022-03-21 UTC 2022-03-26 UTC
#> 4  2022-03-26 UTC 2022-04-05 UTC
#> 5  2022-04-05 UTC 2022-04-10 UTC
#> 6  2022-04-10 UTC 2022-04-15 UTC
#> 7  2022-04-15 UTC 2022-04-25 UTC
#> 8  2022-04-25 UTC 2022-04-30 UTC
#> 9  2022-04-30 UTC 2022-05-10 UTC
#> 10 2022-05-10 UTC 2022-05-15 UTC
#> 11 2022-05-15 UTC 2022-05-20 UTC
#> 12 2022-05-20 UTC 2022-05-25 UTC

Created on 2022-05-29 by the reprex package (v2.0.1)

1 Like

yes it solved.

Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.