0

Thank you for entering this thread, I belive this is a simple question so here it goes.

I have two columns, one for HOURS and one for MINUTES on a 24:60 format, both columns are INTEGERS AND I want to create a column on TIME(?) format, example:

Hour(Integer)        Minutes(Integer)        Time Column
      5                     30                  5:30
      7                     27                  7:27
     11                     05                 11:05
     17                     45                 17:45
     22                     15                 22:15

That´s pretty much it, thank you.

Edit: Shree answers only gives Characters

René Martínez
  • 179
  • 1
  • 3
  • 11

4 Answers4

5

Base R, using @Bruno's data:

paste(df$Hour, df$Minutes, sep=":")
# [1] "5:30"  "7:27"  "11:5"  "17:45" "22:15"

as.POSIXct(paste(df$Hour, df$Minutes, sep=":"), format = "%H:%M", tz = "GMT")
# [1] "2019-06-05 05:30:00 GMT" "2019-06-05 07:27:00 GMT"
# [3] "2019-06-05 11:05:00 GMT" "2019-06-05 17:45:00 GMT"
# [5] "2019-06-05 22:15:00 GMT"

I specifically included tz= in the call, mostly for "good practice" since base R POSIXt functions apply none by default (which means it infers, but does not state, your current tz). It works fine without this.

Note that this will always default to "today" (when you run as.POSIXct), since we are not specifying a day/date in our call.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Thank you so much for your help, if I were to follow your suggestion, I understand that there's no way to hide the YY/MM/DD info? As I just need HH:MM – René Martínez Jun 06 '19 at 02:00
  • You cannot have `POSIXt` in R without it. When you want to display it, you can always use `format` to include just the part you want, but then it will be `character`, and not time/math-friendly. There are libraries/packages that masks this from you, I'm confident. – r2evans Jun 06 '19 at 04:20
4

There are several ways to do this, as you can tell by the amount of responses. But a path I always find easier is to use lubridate::parse_date_time because it is possible to work around this and many other data manipulation issues for this class.

df <- data.frame(Hour = c(5,7,11,17,22), 
             Minutes  = c(30,27,05,45,15))

library(dplyr)
library(lubridate)

df <- df %>% 
  dplyr::mutate(Time = format(strptime(lubridate::parse_date_time(paste(Hour, Minutes, sep = ":"), 'H:M'),
                                       "%Y-%m-%d %H:%M:%S"),'%H:%M'))

# Or in a more didactic way
df <- df %>% 
  dplyr::mutate(Time = paste(Hour, Minutes, sep = ":"),
                Time = lubridate::parse_date_time(Time, 'H:M'),
                Time = format(strptime(Time, "%Y-%m-%d %H:%M:%S"),'%H:%M'))
bbiasi
  • 1,549
  • 2
  • 15
  • 31
  • Your's it's the most elaborate answer and so I have a question (I haven't run your code, I'm writing from my Smartphone) but, Why do I need the last line? ...Format (strptime)... – René Martínez Jun 06 '19 at 02:07
  • For only hour and minute to remain, it is a disadvantage of using `lubridate::parse_date_time`. – bbiasi Jun 06 '19 at 02:38
1
library(lubridate)
library(tidyverse)
library(stringr)
df <- tribble(
~Hour,~Minutes,
5,30,
7,27,
11,05,
17,45,
22,15)

df %>% 
  mutate(time = str_c(Hour,Minutes,sep = ":")) %>% 
  mutate(time = time %>% hm())
Bruno
  • 4,109
  • 1
  • 9
  • 27
0

Here's a tidyverse solution. This just creates a character variable.

library(tidyverse)
library(stringr)

df <- tibble(Hour = c(5,7,11,17,22), Minutes  = c(30,27,05,45,15))

df %>% 
  mutate(Time = str_c(Hour, Minutes, sep = ':'))
Tony Ladson
  • 3,539
  • 1
  • 23
  • 30