Convert UNIX Timestamp to Date Object in R (2 Examples)


In this tutorial, I’ll explain how to convert UNIX epoch time objects to a date object in R programming.

The content of the page looks as follows:

Let’s get started…


Example Data

Consider the example data below:

my_time <- 1412368227                                       # Example timestamp

The previous R code shows our example data – It’s a single timestamp (also called Epoch time, POSIX time, seconds since the Epoch, or UNIX Epoch time) stored in the data object my_time.


Example 1: Converting Timestamp to Date Class

The following syntax shows how to convert a UNIX time object to an object with the Date class. First, we are converting our timestamp to the POSIXct using the as.POSIXct function:

my_time_new1 <- as.POSIXct(my_time, origin = "1970-01-01")  # as.POSIXct function
my_time_new1                                                # Return output
# "2014-10-03 22:30:27 CEST"

As you can see based on the previous output of the RStudio console, our time object changed from a simple and hard to read numeric value to a real date. We can also check the class of our updated data using the class function:

class(my_time_new1)                                         # Check class
# "POSIXct" "POSIXt"

Our new date object has the POSIXct class.

Now, we may convert this POSIXct to the Date class as shown below:

my_time_new2 <- as.Date(my_time_new1)                       # Convert POSIXct to Date
my_time_new2                                                # Return output
# "2014-10-03"

The structure of our data changed again, now showing only the year, month, and day of our date. Let’s check the class of our new data object:

class(my_time_new2)                                         # Check class
# "Date"

As expected: Our final output is a data object with the Date class.


Example 2: Converting Timestamp to Date Using lubridate Package

The following R programming syntax shows an alternative R code compared to Example 1. In this example, we are using the lubridate package to modify our UNIX epoch time. We first need to install and load the lubridate package:

install.packages("lubridate")                               # Install & load lubridate

Now, we can apply the as_datetime(my_time) function to convert our original date variable to a readable format:

as_datetime(my_time)                                        # Apply as_datetime function
# "2014-10-03 20:30:27 UTC"

Whether you want to use the functions of Example 1 or 2 is a matter of taste.


Video, Further Resources & Summary

Would you like to know more about the conversion of UNIX / POSIX time in R? Then you may watch the following video of my YouTube channel. I’m showing the contents of this tutorial in the video:


The YouTube video will be added soon.


Besides the video, you may have a look at some of the related articles of this website. I have published numerous articles already:


Summary: You learned on this page how to change UNIX timestamps to the Date class in R programming. In case you have any additional questions, please let me know in the comments section below.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

2 Comments. Leave new

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.