How to Add Time to POSIXct Object in R (Example Code)

This tutorial explains how to add a time to a POSIXct object in the R programming language.

Example Data

x <- as.POSIXct("2023-03-15")                # Example date in R
x                                            # Display date in RStudio console
# [1] "2023-03-15 CET"

Example: Adding Time to Date Using lubridate Package

install.packages("lubridate")                # Install lubridate package
library("lubridate")                         # Load lubridate
x + hours(18) + minutes(17) + seconds(59)    # Adding time
# [1] "2023-03-15 18:17:59 CET"

Further Resources

Here, you may find some additional resources on topics such as time objects, data objects, and dates.

Leave a Reply

Your email address will not be published.

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