R Return Hours, Minutes & Seconds of Time Object (Example Code)

This tutorial illustrates how to get hours, minutes, and seconds in R.

Example Data

my_time <- c("2017-06-13 11:22:56",    # Creating vector of Date objects
                     "2024-09-05 18:52:07",
                     "2021-12-11 05:04:06")
my_time                                # Showing date vector in RStudio console
# "2017-06-13 11:22:56" "2024-09-05 18:52:07" "2021-12-11 05:04:06"

Example: Applying format Function to Extract Day from Date

install.packages("lubridate")          # Install & load lubridate
library("lubridate")
second(my_time)                        # Applying second() function
# 56  7  6
minute(my_time)                        # Applying minute() function
# 22 52  4
hour(my_time)                          # Applying hour() function
# 11 18  5

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.
You need to agree with the terms to proceed

Menu
Top