How can I convert a UNIX timestamp to a date in R using three different methods?

How can I convert a UNIX timestamp to a date in R using three different methods?

In order to convert a UNIX timestamp to a date in R, there are three main methods that can be used. The first method involves using the “as.POSIXct” function, which converts the timestamp to a POSIXct object and then using the “format” function to specify the desired date format. The second method involves using the “as.Date” function, which converts the timestamp to a Date object and then using the “format” function to specify the desired date format. Lastly, the third method involves using the “strptime” function, which converts the timestamp to a date object and then using the “strftime” function to specify the desired date format. All three methods provide different options for converting a UNIX timestamp to a date in R, allowing for flexibility and ease in data manipulation and analysis.

Convert UNIX Timestamp to Date in R (3 Methods)


You can use one of the following three methods to convert a to a date object in R:

Method 1: Use Base R

#convert UNIX timestamp to date 
as.Date(as.POSIXct(x, origin="1970-01-01"))

Method 2: Use anytime Package

library(anytime)

#convert UNIX timestamp to date
anydate(x)

Method 3: Use lubridate Package

library(lubridate)

#convert UNIX timestamp to date 
as_date(as_datetime(x))

The following examples show how to use each function in practice.

Example 1: Convert Timestamp to Date Using Base R

We can use the following code to convert a UNIX timestamp to a date using only functions from base R:

#define UNIX timestamp
value <- 1648565400

#convert UNIX timestamp to date object
new_date <- as.Date(as.POSIXct(value, origin="1970-01-01"))

#view date object
new_date

[1] "2022-03-29"

#view class of date object
class(new_date)

[1] "Date"

The UNIX timestamp has successfully been converted to a date object.

Example 2: Convert Timestamp to Date Using anytime Package

We can also use the anydate() function from the anytime package to convert a UNIX timestamp to a date object in R:

library(anytime)

#define UNIX timestamp
value <- 1648565400

#convert UNIX timestamp to date object
new_date <- anydate(value)

#view date object
new_date
[1] "2022-03-29"

#view class of date object
class(new_date)[1] "Date"

The UNIX timestamp has successfully been converted to a date object.

Example 3: Convert Timestamp to Date Using lubridate Package

library(lubridate)

#define UNIX timestamp
value <- 1648565400

#convert UNIX timestamp to date object
new_date <- as_date(as_datetime(value))

#view date object
new_date

[1] "2022-03-29"

#view class of date object
class(new_date)

[1] "Date"

Once again, the UNIX timestamp has successfully been converted to a date object.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

Cite this article

stats writer (2024). How can I convert a UNIX timestamp to a date in R using three different methods?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-unix-timestamp-to-a-date-in-r-using-three-different-methods/

stats writer. "How can I convert a UNIX timestamp to a date in R using three different methods?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-unix-timestamp-to-a-date-in-r-using-three-different-methods/.

stats writer. "How can I convert a UNIX timestamp to a date in R using three different methods?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-unix-timestamp-to-a-date-in-r-using-three-different-methods/.

stats writer (2024) 'How can I convert a UNIX timestamp to a date in R using three different methods?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-unix-timestamp-to-a-date-in-r-using-three-different-methods/.

[1] stats writer, "How can I convert a UNIX timestamp to a date in R using three different methods?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I convert a UNIX timestamp to a date in R using three different methods?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
PDF
Scroll to Top