Search code examples
rstringdatetimenumber-formatting

printing time inputs as words in r


I would like to write some code which takes in a time and outputs the words of that time.

time <-  c("5:00", "5:01", "5:10", "5:15", "5:30", "5:40", "5:45", "5:47", "5:28")
words <- c("five o`clock", "one minute past five",
                    "ten minutes past five", "quarter past five", 
                    "half past five", "twenty minutes to six", "quarter to six", 
                    "thirteen minutes to six", "twenty eight minutes past five")


df <- cbind(time, words)
df    

That is for 1 <= 30 minutes use the word "past" and for 30 > "minutes" use to.

I would like to print the time in words for any time format such as the following:

      time   words                           
 [1,] "5:00" "five o`clock"                  
 [2,] "5:01" "one minute past five"          
 [3,] "5:10" "ten minutes past five"         
 [4,] "5:15" "quarter past five"             
 [5,] "5:30" "half past five"                
 [6,] "5:40" "twenty minutes to six"         
 [7,] "5:45" "quarter to six"                
 [8,] "5:47" "thirteen minutes to six"       
 [9,] "5:28" "twenty eight minutes past five"

Solution

  • time <-  c("5:00", "17:45","10:05:00","5:10 pm", "5:01", "5:10:20 AM", "23:45:00",
               "5:10", "5:15", "5:30", "5:40", "5:45", "5:47", "5:28", "00:10:20")
    
    library(dplyr)
    library(lubridate)
    library(qdap)
    
    time %>% data.frame(time=.) %>% 
      mutate(datetime = parse_date_time(time, c("HMS", "HM","IMOp", "IMSOp")),
             h = as.double(hour(datetime)),
             m = minute(datetime),
             h = case_when(h==0 ~ 12,
                           h > 12 ~ h -12,
                           TRUE ~ h)) %>% 
      mutate(TIME_chr = 
            case_when(m == 0 ~ paste(replace_number(h), "o`clock"),
                      m == 15 ~ paste("quarter past", replace_number(h)),
                      m == 30 ~ paste("half past", replace_number(h)),
                      m == 45 ~ paste("quarter to", replace_number(h + 1)),
                      m > 30 ~ paste(replace_number(60-m), "minutes to", replace_number(h+1)),
                      TRUE ~ paste(replace_number(m), "minutes past", replace_number(h)))) %>% 
      select(time, TIME_chr)
    #>          time                       TIME_chr
    #> 1        5:00                   five o`clock
    #> 2       17:45                 quarter to six
    #> 3    10:05:00          five minutes past ten
    #> 4     5:10 pm          ten minutes past five
    #> 5        5:01          one minutes past five
    #> 6  5:10:20 AM          ten minutes past five
    #> 7    23:45:00              quarter to twelve
    #> 8        5:10          ten minutes past five
    #> 9        5:15              quarter past five
    #> 10       5:30                 half past five
    #> 11       5:40          twenty minutes to six
    #> 12       5:45                 quarter to six
    #> 13       5:47        thirteen minutes to six
    #> 14       5:28 twenty eight minutes past five
    #> 15   00:10:20        ten minutes past twelve
    

    Created on 2019-09-12 by the reprex package (v0.3.0)