Search code examples
rtimestampposix

R: Several Timestamp columns merged to Timestamp Series


A data frame contains several TIMESTAMP Columns.

# Create simple example
df <- cbind.data.frame(A=c("A","B","C")
                      ,B=c(1,2,3)
,Timestamp_1=c(as.POSIXct(NA),as.POSIXct("2018-05-04 00:19:41"),as.POSIXct("2018-07-31 22:09:10"))
,Timestamp_2=c(as.POSIXct("2018-05-04 00:18:45"),as.POSIXct("2018-05-05 00:18:43"),as.POSIXct("2018-06-05 00:00:01"))
,Timestamp_3=c(as.POSIXct("2018-05-04 00:19:13"),as.POSIXct("2018-05-05 00:17:00"),as.POSIXct("2018-05-06 00:18:41"))
,C=c("Dog","Cat","Mouse")
)

df

A B         Timestamp_1         Timestamp_2         Timestamp_3     C
1 A 1                <NA> 2018-05-04 00:18:45 2018-05-04 00:19:13   Dog
2 B 2 2018-05-04 00:19:41 2018-05-05 00:18:43 2018-05-05 00:17:00   Cat
3 C 3 2018-07-31 22:09:10 2018-06-05 00:00:01 2018-05-06 00:18:41 Mouse

The result of merging the TIMESTAMP Columns needs to look like

df_Result
  A B         Timestamp_ALL  C
1 A 1                <NA>   Dog
2 A 1 2018-05-04 00:18:45   Dog
3 A 1 2018-05-04 00:19:13   Dog
4 B 2 2018-05-04 00:19:41   Cat
5 B 2 2018-05-05 00:17:00   Cat
6 B 2 2018-05-05 00:18:43   Cat
7 C 3 2018-05-06 00:18:41 Mouse
8 C 3 2018-06-05 00:00:01 Mouse
9 C 3 2018-07-31 22:09:10 Mouse

How to do this in a R-elegant & efficient way? Thanks a million for any advice and idea!


Solution

  • Here is a tidyverse solution using tidyr::gather

    library(tidyverse)
    df %>%
        gather(key, Timestamp_ALL, -A, -B, -C) %>%
        select(A, B, Timestamp_ALL, C, -key) %>%
        arrange(A, B)
    #  A B       Timestamp_ALL     C
    #1 A 1                <NA>   Dog
    #2 A 1 2018-05-04 00:18:45   Dog
    #3 A 1 2018-05-04 00:19:13   Dog
    #4 B 2 2018-05-04 00:19:41   Cat
    #5 B 2 2018-05-05 00:18:43   Cat
    #6 B 2 2018-05-05 00:17:00   Cat
    #7 C 3 2018-07-31 22:09:10 Mouse
    #8 C 3 2018-06-05 00:00:01 Mouse
    #9 C 3 2018-05-06 00:18:41 Mouse
    

    Explanation: gather converts the data.frame from wide to long, the rest is just selecting relevant columns using select, and ordering entries by A then B with arrange.