Add Time to Date Object in R (Example)
This article illustrates how to add a time stamp to a POSIXct object in the R programming language.
Table of contents:
So now the part you have been waiting for – the examples.
Creation of Example Data
The following data is used as basement for this R programming tutorial:
my_date <- as.POSIXct("2022-10-05") # Create example date my_date # Print example date # [1] "2022-10-05 CEST"
The previous output of the RStudio console shows that our example data contains a single date, i.e. the fifth of October of the year 2022.
It is important that our data object is properly formatted as date. Let’s check the class of our data object:
class(my_date) # Class of data object # [1] "POSIXct" "POSIXt"
Our example data is a POSIXct object – looks good. In case you data does not have this class yet, you can use the as.POSIXct function to convert your data to this class.
Example 1: Add Hours, Minutes & Seconds to Date Using lubridate Package
The following R programming syntax illustrates how to add a time to a date object in R using the functions of the lubridate package.
In case we want to use the functions of the lubridate package, we first need to install and load lubridate:
install.packages("lubridate") # Install & load lubridate package library("lubridate")
Next, we can apply the hours, minutes, and seconds functions to add the corresponding times to our date object:
my_date_time1 <- my_date + # Add hours, minutes & seconds hours(5) + minutes(20) + seconds(35) my_date_time1 # Print date with time # [1] "2022-10-05 05:20:35 CEST"
Have a look at the previous output of the RStudio console: We have created a data object containing a date and time.
Example 2: Merge Date & Time Objects Using as.POSIXct Function & format Argument
Example 2 explains how to merge an already existing time object (or data frame column) to a date object.
First, we have to create such a time object in R:
my_time <- "10:40:25" # Create example time my_time # Print example time # [1] "10:40:25"
Next, we can use the as.POSIXct function and format argument to combine our date and time objects:
my_date_time2 <- as.POSIXct(paste(my_date, my_time), # Add hours, minutes & seconds format = "%Y-%m-%d %H:%M:%S") my_date_time2 # Print date with time # [1] "2022-10-05 10:40:25 CEST"
The previous output of the RStudio console shows our date-time object.
Video, Further Resources & Summary
Have a look at the following video tutorial of my YouTube channel. In the video, I show the content of this article in R.
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 may have a look at some of the related R articles of this homepage:
- Extract Hours, Minutes & Seconds from Date & Time Object
- Convert Data Frame with Date Column to Time Series Object
- Add and Subtract Days to from Date
- Convert Date to Numeric Time Object
- All R Programming Examples
You have learned in this article how to merge times and dates in R programming. In case you have any additional questions, let me know in the comments section.
Statistics Globe Newsletter