## R - 将整数转换为小时/分钟内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (6)

``````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
``````

### 2 个回答

```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())```

``````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'))
``````