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"

Example: Splitting Date/Time Object

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"

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top