# Convert duration to hours

Below is the result of a data I have.

Month Total_Dur20
March 12942s (~3.6 hours)
April 19185s (~5.33 hours)
May 18744s (~5.21 hours)
June 20855s (~5.79 hours)
July 18560s (~5.16 hours)
August 39327s (~10.92 hours)
September 37625s (~10.45 hours)
October 53851s (~14.96 hours)
November 58633s (~16.3 hours)
December 57119s (~15.87 hours)

When I plot a graph, it is too cluttered because the data is being displayed in total seconds & hours. I just want it to be in hours so the data needs to display only in hours. Please help.

Can you please share a small part of the data set in a copy-paste friendly format?

In case you don't know how to do it, there are many options, which include:

1. If you have stored the data set in some R object, dput function is very handy.

2. In case the data set is in a spreadsheet, check out the datapasta package. Take a look at this link.

I am unable to figure out with the instructions you have shared. Do you have a solution to my problem? The graph, when I plot comes as below. It is too cluttered because it mentions the seconds & hours while I want it to only display hours.

I can't give you any meaningful advice without having a REPRoducible EXample (reprex) to work with, otherwise I would be just guessing.

If you take the time to click on the link, read the reprex guide and make one, I will take a look at your problem.

structure(list(Month = structure(3:7, .Label = c("January", "February",
"March", "April", "May", "June", "July", "August", "September",
"October", "November", "December"), class = "factor"), Total_Dur20 = new("Duration",
.Data = c(12942, 19185, 18744, 20855, 18560))), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))

Code is as below:
WorkoutDur20 <- wd20 %>%
group_by(Month) %>%
separate(Time, into = c("hours", "minutes", "seconds"), sep = ":", convert = TRUE) %>%
summarise(Total_Dur20 = duration(hour = sum(hours), minute = sum(minutes), second = sum(seconds)))

WorkoutDur20 <- WorkoutDur20 %>%
mutate(
Month = factor(Month, levels = month.name)
) %>%
arrange(Month)

The graph has already been shared. Is this proper enough for you to help?

This could be what you are looking for:

``````# Data Frame with similar structure as yours:
WorkoutDur20 <-
data.frame(Month = c("March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"),
Total_Dur20 = c("12942s (~3.6 hours)", "19185s (~5.33 hours)",
"18744s (~5.21 hours)", "20855s (~5.79 hours)",
"18560s (~5.16 hours)", "39327s (~10.92 hours)",
"37625s (~10.45 hours)", "53851s (~14.96 hours)",
"58633s (~16.3 hours)", "57199s (~15.87 hours)"))

# Substitute characters with blank space if not needed using regex
WorkoutDur20\$Total_Dur20 <- gsub("[s|(|)|hours]", "", WorkoutDur20\$Total_Dur20)

# Separate based on a specific value: Once again, you may need to use regex.
WorkoutDur20 <- separate(WorkoutDur20, Total_Dur20, into = c("Seconds", "Hours"), sep = " ~")

# After separating, you need to convert back to numeric
WorkoutDur20\$Seconds <- as.numeric(WorkoutDur20\$Seconds)
WorkoutDur20\$Hours <- as.numeric(WorkoutDur20\$Hours)

# The fill seems redundant when you already have bars that tell the same story, but if you want it, you do you.
ggplot(WorkoutDur20, aes(x = factor(Month, levels = month.name), y = Hours #, fill = Hours
)) +
geom_col() +

geom_text(aes(label = Hours), nudge_y = 0.5) +

labs(x = "Month", y = "Hours", title = "Total Duration Worked Out Each Month in 2020") +

theme(plot.title = element_text(hjust = 0.5))
``````

Yes this one is perfect. Thank you

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.