-1

i have a data file in which i have to work with data columns on time, namely hours and minutes, but the stored data is 1900 to represent 19h00 ', 1845 to It's 18h45 'but there are also 930s for 930 and 455 for 4h5, so now I do not know how to handle the same form, if I just want to split the number of hours. How I have to do?

  • 3
    It would be easier to help if you gave some sample data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that shows the different types of input and the expected output for each input. That way possible solutions can be tested and verified. – MrFlick Oct 11 '18 at 21:51
  • 1
    Welcome to Stack Overflow! You may want to check out https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Without more detail, it's going to be very difficult for anyone to help you. – M.Qasim Oct 11 '18 at 21:52
  • 1
    You can handle them as string. Split them (last two digit and the rest). Last two digit would be minutes and first two or first one is the hour. – M-- Oct 11 '18 at 21:53
  • 1
    How does 455 translate to 4h5? – Chabo Oct 11 '18 at 21:57
  • 1
    Hi Tuyen, there is a great package called `lubridate` that may be helpful for you. If you provide us with a sample of your data in a form we can cut and paste into our R session, plus a specific description of what the output should look like, it will make easier for us to help. Thanks :) – mysteRious Oct 11 '18 at 21:57
  • I am working on a package for weird time stripping instances, one of those functions is laid out in an answer here https://stackoverflow.com/questions/52412603/function-in-r-to-change-numbers-into-time-format-without-dots-in-excel-file/52413206#52413206 Not sure if it will help. – Chabo Oct 11 '18 at 22:27
  • Related - https://stackoverflow.com/questions/41581164/convert-time-values-to-numeric-while-keeping-time-characteristics/41581393 – thelatemail Oct 11 '18 at 22:41

1 Answers1

1

The hours without minutes could be calculated as:

s <- c(1900, 1845, 930, 930, 455) # input

s %/% 100
## [1] 19 18  9  9  4

or the number of hours (using fractional hours for minutes) is:

s %/% 100 + (s %% 100) / 60
## [1] 19.000000 18.750000  9.500000  9.500000  4.916667

chron times

Another approach is to convert s to a chron "times" object and from that extract the hours or the hours and minutes. The last alternative below uses the fact that "times" objects are stored as a fractions of a day.

library(chron)

tt <- times(sub("(..)$", ":\\1:00", s)); tt
## [1] 19:00:00 18:45:00 09:30:00 09:30:00 04:55:00

# alternative    
times((s %/% 100) / 24 + (s %% 100) / (24 * 60))
## [1] 19:00:00 18:45:00 09:30:00 09:30:00 04:55:00

hours(tt)
## [1] 19 18  9  9  4

minutes(tt)
## [1]  0 45 30 30 55

hours(tt) + minutes(tt) / 60
## [1] 19.000000 18.750000  9.500000  9.500000  4.916667

24 * as.numeric(tt)
## [1] 19.000000 18.750000  9.500000  9.500000  4.916667
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341