I've got a tool that I run to make calculations about running times of certain programs, giving me many times the same 1 row table with the resulting numbers (6 columns, with a header with column names). I am doing this for three programs and I have to sum/combine the result as if it was one program, not three. (for example, if program 1 takes 1 hour to run, program 2 takes 1 hour and program 3 takes 3 hours, then the result is to sum up and have 5 hours total).
5 of the values from such output tables look like "304.34" (decimal numbers, usually in the order of the hundreds, and have to be summed), but one column has time with the following format: 01:04:56 . I would like to make an R script that takes in 3 tables (for the three programs mentioned), and sums that to produce one table. (with the same header)
The problem is, I am having trouble to think of a function that can do that and sum the time column properly as well as the others. Thank you!
**example:
df <- read.table(text = "col1 col2 col3 col4 col5 col6_time
300.45 201.4 100.11 100.35 101 01:10:05
300 202.1 105.20 101.10 100 01:00:01
250.10 200.4 101.21 102 100 00:45:23", header = T)
**desired output (sum values, keep header):**
col1 col2 col3 col4 col5 col6_time
1 850.55 603.9 306.52 303.45 301 02:55:28
You can turn the character columns to a period object and sum it.
library(dplyr)
library(lubridate)
df %>%
summarise(across(where(is.numeric), sum),
across(where(is.character),
~format(parse_date_time(seconds_to_period(sum(period_to_seconds(hms(.)))), "HMS"), '%T')))
# col1 col2 col3 col4 col5 col6_time
#1 850.55 603.9 306.52 303.45 301 02:55:29