我需要匹配三个时间不同的数据源。我尝试使用hms和timechange将55分钟添加到列中,但没有成功。
library(tidyverse)
library(timechange)
library(hms)
df1 <- data.frame(ID = c(1, 2, 3),
time = as.hms(c('18:12:55', '18:12:57', '18:12:59')))
df1 %>%
mutate(time.f = time_add(time, minutes = "00:55:00"))
#> Error: Problem with `mutate()` input `time.f`.
#> x Unsupported date-time class 'hms'Unsupported date-time class 'difftime'
#> i Input `time.f` is `time_add(time, minutes = "00:55:00")`.
由reprex软件包(v0.3.0)创建于2021-02-02
该time_add
函数仅支持class对象datetime
。数据帧中的时间列类别为hms
和difftime
,因此错误消息指出不支持'hms'和'difftime'。
library(tidyverse)
library(timechange)
library(hms)
df1 <- data.frame(ID = c(1, 2, 3),
time = as_hms(c('18:12:55', '18:12:57', '18:12:59')))
class(df1$time)
[1] "hms" "difftime"
因此,只需使用将55分钟转换为一个hms
对象as_hms
,将其添加到time
列中,然后将结果转换为一个hms
对象即可。
df1 %>%
mutate(time.f = as_hms(time + as_hms('00:55:00')))
ID time time.f
1 1 18:12:55 19:07:55
2 2 18:12:57 19:07:57
3 3 18:12:59 19:07:59
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句