The Smart Chef

Numeric Time to Date

Timestamps in R are like unruly teenagers - they always seem to be up to something! Whether you're trying to convert them to a different format, compare them to other dates, or extract specific parts of the date and time, working with timestamps in R can be a real adventure.

But don't worry, there's a whole library of functions ready to help you wrangle those timestamps into submission. So, grab a cup of coffee (or maybe a bottle of wine), and get ready to have some fun with timestamps in R!

If you have a numeric value that represents a date and time, you can use the as.POSIXct function to convert it to a date-time object in R. For example:

Numeric Time to Char Date

  

## Set x to the current system time
x <- as.numeric(Sys.time())

## Convert x to a POSIXct variable
x <- as.POSIXct(x, origin = "1970-01-01")

## View x
> x
[1] "2023-02-06 11:30:05 EST"


## From here you can then convert to a character if needed for reporting purposes
x <- as.character(x)