【发布时间】:2021-11-08 00:05:08
【问题描述】:
我想知道我是否遗漏了什么?!
我想知道:有没有更好/更短的方法从日期时间对象中获取分钟数:
目前学习的课程:
Extract time (HMS) from lubridate date time object?
converting from hms to hour:minute in r, or rounding in minutes from hms
R: Convert hours:minutes:seconds
我的小标题:
df <- structure(list(dttm = structure(c(-2209068000, -2209069200, -2209061520,
-2209064100, -2209065240), tzone = "UTC", class = c("POSIXct",
"POSIXt"))), row.names = c(NA, -5L), class = c("tbl_df", "tbl",
"data.frame"))
# A tibble: 5 x 1
dttm
<dttm>
1 1899-12-31 02:00:00
2 1899-12-31 01:40:00
3 1899-12-31 03:48:00
4 1899-12-31 03:05:00
5 1899-12-31 02:46:00
我想添加一个以分钟为整数的新列:
到目前为止我的方法:
library(dplyr)
library(lubridate) # ymd_hms
library(hms) # as_hms
library(chron) # times
test %>%
mutate(dttm_min = as_hms(ymd_hms(dttm)),
dttm_min = 60*24*as.numeric(times(dttm_min)))
# A tibble: 5 x 2
dttm dttm_min
<dttm> <dbl>
1 1899-12-31 02:00:00 120
2 1899-12-31 01:40:00 100
3 1899-12-31 03:48:00 228
4 1899-12-31 03:05:00 185
5 1899-12-31 02:46:00 166
这给了我想要的结果,但我需要 3 个包。有没有更直接的方法?
【问题讨论】:
-
这是一个很好的答案。因此,我链接了这个答案。就我个人而言,我真的很难在我的大脑中获得
as.POSIXlt。至少在这个阶段。所以我想问问其他方法。我希望这没问题!
标签: r datetime time lubridate chron