Extract Hours, Minutes & Seconds from Date & Time Object in R (Example)
In this tutorial, I’ll explain how to return only hours, minutes, and seconds from a date/time object in the R programming language.
The tutorial will consist of these content blocks:
With that, let’s do this.
Creation of Exemplifying Data
Consider the following example data:
x <- "2023-08-07 03:22:56" # Create example date & time x # Print example date & time # "2023-08-07 03:22:56"
The previous output of the RStudio console shows that our example data object contains a single character string showing a date and a time.
Example 1: Extracting Hour, Minute & Seconds from Date & Time Object Using lubridate Package
The R syntax below explains how to extract time metrics from a character string using the lubridate package.
We first have to install and load the lubridate package, if we want to use the functions that are included in the package:
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate package
Now, we can extract the hours…
hour(x) # Extract hour # 3
…minutes…
minute(x) # Extract minute # 22
…and seconds…
second(x) # Extract second # 56
…from our date and time character string.
Note that the output values of the previous R code have the numeric class. You may convert these outputs to other data types such as the character class or to the factor class.
Example 2: Extracting Hour, Minute & Seconds from Date & Time Object Using hms Package
Alternatively to the lubridate package, we may also use the hms package to extract hours, minutes, and from a date.
First, we can use the format and as.POSIXct functions to remove the date component from our data:
x_time <- format(as.POSIXct(x), # Extract hours, minutes & seconds format = "%H:%M:%S") x_time # Print hours, minutes & seconds
# [1] “03:22:56”
Looks good! However, at this point our output has the character class:
class(x_time) # Check class of data object # [1] "character"
The character class cannot be used to make calculations with our hours, minutes, and seconds. For that reason, it makes sense to convert the previous output to the hms class.
For this, we first have to install and load the hms package:
install.packages("hms") # Install hms package library("hms") # Load hms package
Next, we can apply the as_hms function to convert our character string to the hms data type:
x_hms <- as_hms(x_time) # Convert character to hms x_hms # Print hours, minutes & seconds # 03:22:56
Let’s test the class once again:
class(x_hms) # Check class of data object # [1] "hms" "difftime"
Using the hms data type, we can make calculations based on our hours, minutes, and seconds.
Video & Further Resources
Some time ago, I have released a video on my YouTube channel, which illustrates the examples of this tutorial. You can find the video below.
The YouTube video will be added soon.
Furthermore, you might have a look at the other articles of my website:
- Convert Date to Numeric Time Object
- Extract Day from Date
- Extract Month from Date
- Extract Year from Date
- R Programming Tutorials
You learned on this page how to get hours, minutes, and seconds in R programming. Kindly let me know in the comments section, if you have additional questions.
Statistics Globe Newsletter