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 library("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:
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.
Besides the video, you may have a look at some of the related articles of this website. I have published numerous articles already:
- Convert Character String to Date Object
- Convert Date to Numeric Time Object
- as.Date Function in R
- Convert Date to Day of Week
- How to Create a Range of Dates
- The R Programming Language
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.
Statistics Globe Newsletter
2 Comments. Leave new
This doesn’t seem to work with an alphanumeric timestamp like 5FD5A62E. Do you know of any modifications to the code that would work with this?
Thank you!
Hey Francisco,
That’s actually a very good question. I have never done this myself, but I have found this thread on Stack Overflow, which seems to answer your question: https://stackoverflow.com/questions/47122935/how-to-convert-a-character-timestamp-into-a-date-time-object-in-r
I hope that helps!
Joachim