How can you convert a character to a time (hms) in RStudio?

Hello! :slight_smile:

I've been struggling all morning with this. I wanted to practice a bit with visualisations in R with a very basic dataset: one column is for "days", one column is for "total study hours" (expressed in hours, minutes and seconds although seconds are always at zero).

I've loaded the dataset in RStudio and the study hours column was identified as "character" even if the format is different. The format I need is HH:MM and I need it in a way that can be represented as data points on a line chart! (the dataset does contain some "0" values, I assumed that the software with still consider them as part of the data - why wouldn't they?) So far, al I got is a flat line!

I've tried everything:

  • hms()
  • as.character.Date()
  • strptime()
  • format()
  • apparently there is a "as.time()" function but I could not find it
  • difftime()

Can anyone help me? The documentation online is not really helpful and since I'm a beginner in R (I started one week ago), I'm quite desperate!

Thank you in advance for your answers! :slight_smile:

Welcome to the forum

Probably the best idea is to supply us widh the gode you have been trying and some sample data
Have a look at

Also a handy way to supply sample data is to use the dput() function. See ?dput. If you have a very large dataset then something like head(dput(myfile), 100) will likely supply enough data for us to work with.

Hey, thanks! :slight_smile:

Sorry, I was kind of bummed that there was no documentation online on a way to solve this issue. This is the output of dput:

structure(list(day = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30), study_hours = c("7.30.00", "8.30.00", "0.00.00", "0.00.00",
"0.00.00", "8.00.00", "7.00.00", "8.00.00", "0.00.00", "7.30.00",
"4.00.00", "8.00.00", "6.30.00", "7.00.00", "8.00.00", "6.30.00",
"0.00.00", "4.00.00", "4.00.00", "7.20.00", "5.30.00", "5.30.00",
"6.30.00", "0.00.00", "0.00.00", "6.00.00", "7.40.00", "8.00.00",
"5.00.00", "7.00.00")), class = c("spec_tbl_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -30L), spec = structure(list(
cols = list(day = structure(list(), class = c("collector_double",
"collector")), study_hours = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))

On another note, the best I got so far was to convert the whole "study_hours" to "NA" values; in another attempt I managed to change the format but ggplot created a flat line where it was to be a line graph... :sweat_smile:

Thanks for the data.

Well something is happening :slight_smile:

I think you are tackling a fairly nasty little problem for someone with a week's experience.
I find dealing with times and dates can get complicated fast so what I am trying is probably not optional but it gives us a graph.

dat1 <-  structure(list(day = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
          13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
         29, 30), study_hours = c("7.30.00", "8.30.00", "0.00.00", "0.00.00",
        "0.00.00", "8.00.00", "7.00.00", "8.00.00", "0.00.00", "7.30.00",
        "4.00.00", "8.00.00", "6.30.00", "7.00.00", "8.00.00", "6.30.00",
        "0.00.00", "4.00.00", "4.00.00", "7.20.00", "5.30.00", "5.30.00",
       "6.30.00", "0.00.00", "0.00.00", "6.00.00", "7.40.00", "8.00.00",
       "5.00.00", "7.00.00")), class = c("spec_tbl_df", "tbl_df", "tbl",
        "data.frame"), row.names = c(NA, -30L), spec = structure(list(
         cols = list(day = structure(list(), class = c("collector_double",
        "collector")), study_hours = structure(list(), class = c("collector_character",
        "collector"))), default = structure(list(), class = c("collector_guess",
        "collector")), skip = 1L), class = "col_spec"))


library(lubridate)
library(ggplot2)

# Convert times from "7.30.00" to "7:30:00 format
dat1$study_hours <-   gsub("\\.", ":", dat1$study_hours)

# First convert the character data to a *lubricate* hms object and
# then gonvert to a duration using *as.duration*. 
dat1$mytime <-  as.duration(hms(dat1$study_hours))

# plot with basic graphics
with(dat1, plot(day, mytime))

# plot with *ggplot2*
ggplot(dat1, aes(day, mytime)) + geom_point()





















2 Likes

Wow!!! Thanks, John! That did the trick! :flushed:

I have so much to learn! And it seemed so easy on the Google Data Analyst course! :thinking:

Thanks again!!! :blush:

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.