Divide Date-Time Object into Two Separate Variables in R (Example Code)
In this article you’ll learn how to divide a date-time object into two different variables in the R programming language.
Example Data
x <- "2022/07/21 10:30:45" # Example date/time x # Display example data in RStudio console # [1] "2022/07/21 10:30:45" |
x <- "2022/07/21 10:30:45" # Example date/time x # Display example data in RStudio console # [1] "2022/07/21 10:30:45"
Example: Splitting Date/Time Object
x_date <- as.Date(x) # Applying as.Date function x_date # [1] "2022-07-21" |
x_date <- as.Date(x) # Applying as.Date function x_date # [1] "2022-07-21"
x_time <- format(as.POSIXct(x), format = "%H:%M:%S") # Applying format function x_time # [1] "10:30:45" |
x_time <- format(as.POSIXct(x), format = "%H:%M:%S") # Applying format function x_time # [1] "10:30:45"