Home > OS >  R:how to transform or concatenate dates
R:how to transform or concatenate dates

Time:05-26

I have such dataset

d1=structure(list(dats = c(20220516L, 20220516L, 20220516L), t1 = c(10L, 
10L, 11L), t2 = c(20L, 20L, 21L)), class = "data.frame", row.names = c(NA, 
-3L))

dats is date (YYYYMMDD) t1,t2 are hours. I need to transform this data into this format?

                 dt_start                  dt_end
1 2022-05-16 10:00:00.000 2022-05-30 20:00:00.000
2 2022-05-16 10:00:00.000 2022-05-30 20:00:00.000
3 2022-05-16 11:00:00.000 2022-05-31 21:00:00.000

What is the easiest way to transform (concatenate) columns to get the desired result? Thank you.

CodePudding user response:

You can use lubridate

library(lubridate)

data.frame(dt_start = ymd(d1$dats)   hours(d1$t1),
           dt_end   = ymd(d1$dats)   hours(d1$t2))
#>              dt_start              dt_end
#> 1 2022-05-16 10:00:00 2022-05-16 20:00:00
#> 2 2022-05-16 10:00:00 2022-05-16 20:00:00
#> 3 2022-05-16 11:00:00 2022-05-16 21:00:00

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

  •  Tags:  
  • r
  • Related