Calculate Sum & Mean of Hours, Minutes & Seconds in R (2 Examples)
In this tutorial, I’ll explain how to get the sum and mean of a time object in the R programming language.
The page contains two examples for the calculation of the sum and mean of a time object. To be more specific, the content of the tutorial looks as follows:
With that, here’s how to do it…
The first step is to create some example data:
my_time <- c("10:05:45", "07:35:51", "17:12:12", # Create example times "13:18:45", "10:53:54", "06:14:25") my_time # Print example times #  "10:05:45" "07:35:51" "17:12:12" "13:18:45" "10:53:54" "06:14:25"
Have a look at the previous output of the RStudio console. It shows that our exemplifying data is a vector of character strings containing hours, minutes, and seconds.
Example 1: Calculate Sum of Hours, Minutes & Seconds
Example 1 shows how to return the sum of a vector of times in R.
You might say: This should be easy, I’m just using the sum function!
However, unfortunately it’s not as simple as it seems. Let’s apply the sum function to our data:
sum(my_time) # Try to return sum # Error in sum(my_time) : invalid 'type' (character) of argument
As you can see, the error message “Error in sum(my_time) : invalid ‘type’ (character) of argument” has been returned.
The reason for this is that our data object is not formatted as time properly – currently it’s a character string.
In order to convert this character string to a proper time object, we can use the functions of the lubridate package.
If we want to use the functions of the lubridate package, we first need to install and load lubridate:
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate package
Next, we can apply the hms function to convert our character string to the Period class:
my_time_hms <- hms(my_time) # Convert time to Period object my_time_hms # Print time as Period object #  "10H 5M 45S" "7H 35M 51S" "17H 12M 12S" "13H 18M 45S" "10H 53M 54S" "6H 14M 25S"
So far so good, but we can still not calculate the proper sum of time!
sum(my_time_hms) # Sum still doesn't work #  232
The reason for this wrong result is that we have to convert our Period object to seconds (i.e. the smallest metric) first:
my_time_seconds <- period_to_seconds(my_time_hms) # Convert to seconds my_time_seconds # Print seconds #  36345 27351 61932 47925 39234 22465
Next, we can calculate the sum of seconds using the sum function:
my_time_seconds_sum <- sum(my_time_seconds) # Calculate sum of seconds my_time_seconds_sum # Print sum of seconds #  235252
We can now convert this sum of seconds back to a Period with hours, minutes, and seconds:
my_time_sum <- seconds_to_period(my_time_seconds_sum) # Convert seconds to Period my_time_sum # Print final result #  "2d 17H 20M 52S
The previous output shows our final result: The sum of our vector of times is 2 days, 17 hours, 20 minutes, and 52 seconds.
Example 2: Calculate Average of Hours, Minutes & Seconds
Example 2 explains how to calculate the mean of a time object that contains hours, minutes, and seconds.
For this, we can use a similar approach as in Example 1. However, this time I’ll combine all functions that we have used in Example 1.
Note that we are using the mean function instead of the sum function:
my_time_mean <- seconds_to_period( # Mean of hours-minutes-seconds mean( period_to_seconds( hms( my_time)))) my_time_mean # Print mean #  "10H 53M 28.6666666666642S"
As you can see, the mean of our vector of times is 10 hours, 53 minutes, and 28.6666666666642 seconds.
Video, Further Resources & Summary
I have recently published a video tutorial on my YouTube channel, which shows the contents of this article. Please find the video below:
In addition, you could read the other articles which I have published on www.statisticsglobe.com:
- Time Difference Between Dates in Weeks, Days, Hours, Minutes & Seconds
- Calculate Moving Average, Maximum, Median & Sum of Time Series
- Extract Hours, Minutes & Seconds from Date & Time Object
- R Programming Tutorials
Summary: In this R tutorial you have learned how to calculate the sum and mean of a time object. If you have any additional comments and/or questions, let me know in the comments.