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 data frame split date time column separate variables r

 

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 data frame split date time column separate variables r

 

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.

 

 

In addition, you might have a look at the related articles on my website. Some tutorials can be found below.

 

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.

 

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

    Reply
    • 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

      Reply
  • Rob van Mechelen
    September 16, 2022 12:43 pm

    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

    Reply
    • 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

      Reply
  • 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"))

    Reply

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.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top