首页 > 解决方案 > 将带有“x 小时 y 分钟”的时间格式转换为数字

问题描述

我有一个变量表示事件发生后的时间,它的编码如下:

time_since_event <- c("18 m", "56 m", "2 h 30 m", "18 h 2 m")

我希望它是数字并始终指示事件发生后的分钟数:

time_since_event_min <- c(18,56,150,1082)

标签: rtimetimestamptransformminute

解决方案


您可以使用lubridate功能:

library(lubridate)
(time_since_event %>% toupper %>% period %>% period_to_seconds())/60
#[1]   18   56  150 1082

推荐阅读