1

I am wanting to create a startime and endtime column from my current datetime column in R. My data has been grouped by ID.

Here is the data:

                    ID                    DATETIMEUTC


                    A                     12/17/2019 9:46:04 PM
                    A                     12/17/2019 9:46:05 PM                                                
                    A                     12/18/2019 2:34:56 AM
                    A                     12/18/2019 2:34:58 AM 

I am wanting this outcome:

           ID                   StartTime                       EndTime

           A                    12/17/2019 9:46:04 PM           12/17/2019 9:46:05 PM
           A                    12/18/2019 2:34:56 AM           12/18/2019 2:34:58 AM

Here is the code that I am writing to try and achieve this:

            library(dplyr)

            df %>%
            group_by(id) %>%
            mutate(start=date, stop=lead(start, default=end[1]))

This command is not yielding the desired result. I am still researching this. Any suggestions is greatly appreciated!

Tanisha Hudson

Lynn
  • 4,292
  • 5
  • 21
  • 44
  • My apologies. I have adjusted the original data. I wish to ultimately compare two separate datasets that will have starttime, endtime and ID columns. – Lynn Dec 31 '19 at 03:32

1 Answers1

1

We can create a new column with alternate values of c('StartTime', 'EndTime'), group by ID, create a unique row number for each group and spread the data in wide format.

library(dplyr)
df %>%
  group_by(ID, col = rep(c('StartTime', 'EndTime'), length.out = n())) %>%
  mutate(id = row_number()) %>%
  tidyr::pivot_wider(names_from = col, values_from = DATETIMEUTC) %>%
  ungroup() %>%
  select(-id)

# A tibble: 2 x 3
#  ID    StartTime             EndTime              
#  <fct> <fct>                 <fct>                
#1 A     12/17/2019 9:46:04 PM 12/17/2019 9:46:05 PM
#2 A     12/18/2019 2:34:56 AM 12/18/2019 2:34:58 AM

data

df <- structure(list(ID = structure(c(1L, 1L, 1L, 1L), .Label = "A", 
class = "factor"), DATETIMEUTC = structure(1:4, .Label = c("12/17/2019 9:46:04 PM", 
"12/17/2019 9:46:05 PM", "12/18/2019 2:34:56 AM", "12/18/2019 2:34:58 AM"
), class = "factor")), class = "data.frame", row.names = c(NA, -4L))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213