How to Split a Date-Time Column into Separate Variables in R (Example)
This tutorial illustrates how to divide a date-time object into two different variables in R programming.
The tutorial will consist of one example for the splitting of date-times. To be more precise, the post looks as follows:
So now the part you have been waiting for – the example.
Example Data
To begin with, let’s construct some example data in R:
data <- data.frame(values = 11:13, # Create example data frame all = c("2023/05/01 11:34:25", "2022/10/14 09:10:30", "2020/07/08 05:05:05")) data # Print example data frame
Table 1 visualizes that our example data is made of three data points and two columns. The variable all contains a dates and times.
Example: Add Date & Time Variables to Data Frame Using as.Date, format & as.POSIXct Functions
This example explains how to split our date-time column into two separate columns.
In a first step, we can use the as.Date function to add a date variable to our data frame:
data$date <- as.Date(data$all) # Add date column
Next, we can apply the format and as.POSIXct functions to add a time variable to our data:
data$time <- format(as.POSIXct(data$all), # Add time column format = "%H:%M:%S")
Let’s have a look at our updated data frame:
data # Print updated data frame
Table 2 reveals the output of the previous R syntax: Our input data frame plus a date and a time column.
Video, Further Resources & Summary
Do you want to learn more about dates and times in R? Then you could watch the following video of my YouTube channel. In the video, I illustrate the contents of this page in RStudio.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might have a look at the related articles on my website. Some tutorials can be found below.
- Convert Dates to Year/Quarter Format
- Add Time to Date Object in R
- Get Week Number of Date in R
- Split Data Frame Variable into Multiple Columns
- Split Data Frame into List of Data Frames Based On ID Column
- Introduction to R
In this R tutorial you have learned how to separate a date-time data object. Don’t hesitate to tell me about it in the comments section, in case you have further questions.
Statistics Globe Newsletter
8 Comments. Leave new
Hello Joeachim,
Thank you for simplifying this. However for slightly larger data sets, how do I apply this. do I have to list over 50 times as a vector before I can split it. do advise a shortcut please.
Thank you.
Amaka from Nigeria
Hey Amaka,
This code should also work for larger data sets. Have you already tried it? Did you receive any error messages?
Greetings from Germany to Nigeria!
Joachim
It does not work for this
data <- data.frame(values = 11:13, # Create example data frame
all = c("4/28/2016 12:00:00 AM",
"2022/10/14 09:10:30 PM",
"2020/07/08 05:05:05 AM"))
data # Print example data frame
data$date <- as.Date(data$all)
error in charToDate
Hi Rob,
This is because your dates are formatted differently, i.e. the first date has the format month/day/year and the other two dates have the format year/month/day. You would first have to fix this to make the as.Date function work.
Regards,
Joachim
Hi Joachim…….
I was trying to seperate date time column to two separate columns( date column & time column)
I was trying to apply the below example for a large dataset like 5 million rows. In the place of (values = 1:5000000, all = c(………….)) # I am not sure what to write in the c ( ). Can you guide me on this. Thanks.
data <- data.frame(values = 11:13, # Create example data frame
all = c("2023/05/01 11:34:25",
"2022/10/14 09:10:30",
"2020/07/08 05:05:05"))
Hello Pavan,
If I get you correctly, you should use your imported data instead of creating it by yourself like in the example. After you import the data, you can use the name of the variable containing date-times in replacement with “all” in the given example. Such as data$date <- as.Date(data$your_vairable_name). Let me know if you have any further questions. Regards, Cansu
thank you
Welcome!