1

I have the following data:

library(tidyverse)

d1 <- data_frame(
time= c("1899-12-31 02:21:00 UTC", "1899-12-31 05:56:00 UTC"),
number = c(1, 2))

I would like to be able to covert the time variable to be in minutes in a new column so that the dataframe looks like this:

d1 <- data_frame(
time= c("1899-12-31 02:21:00 UTC", "1899-12-31 05:56:00 UTC"),
number = c(1, 2),
minutes = c("141", "356"))

I can do this using lubridate if my time variable is 02:21:00 and 05:56:00 however when I'm using read_excel() to load in the data it's turning my variable into POSIXct, eg 1899-12-31 02:21:00 UTC and 1899-12-31 05:56:00 UTC which is making it hard to convert.

I need to use read_excel() to read the data in so it's not an option to convert to a .csv and do it that way (which would stop it reading it as a POSIXct).

markus
  • 23,189
  • 5
  • 29
  • 47
Mrmoleje
  • 448
  • 1
  • 5
  • 20
  • Are you using a special form of `data_frame`? Or just a typo? – Saurabh Chauhan Aug 22 '18 at 13:37
  • Apologies - have updated with the library – Mrmoleje Aug 22 '18 at 13:39
  • 1
    Do you mean `library(lubridate); mutate(d1, minutes = hour(time) * 60 + minute(time))` ? – markus Aug 22 '18 at 13:46
  • Yeah that's it actually. I think I might delete this actually as I think there was an issue with the library not updating and this not working previous to posting. Unless you think it's worth leaving up @markus – Mrmoleje Aug 22 '18 at 13:53
  • Don't delete it. It might help others in the future. Found a possible dupe. [Still, don't delete it.](https://meta.stackoverflow.com/questions/265736/should-i-delete-my-question-if-it-is-marked-as-a-duplicate) – markus Aug 22 '18 at 13:55
  • 1
    Possible duplicate of [extract hours and seconds from POSIXct for plotting purposes in R](https://stackoverflow.com/questions/10705328/extract-hours-and-seconds-from-posixct-for-plotting-purposes-in-r) – markus Aug 22 '18 at 13:59

0 Answers0