小时/分钟使用lubridate从datetime添加一小时
我想从datetime中提取小时/分钟/秒。数据帧示例:小时/分钟使用lubridate从datetime添加一小时,r,dplyr,lubridate,tibble,R,Dplyr,Lubridate,Tibble,我想从datetime中提取小时/分钟/秒。数据帧示例: df <- structure(list(Study_date_time_PACS = structure(c(1515146548, 1515146548, 1514970658, 1514970658, 1515151732, 1515151732, 1517476589, 1517476589, 1543848246, 1543848246),
df <- structure(list(Study_date_time_PACS = structure(c(1515146548, 1515146548, 1514970658, 1514970658, 1515151732, 1515151732, 1517476589, 1517476589, 1543848246, 1543848246),
class = c("POSIXct", "POSIXt"),
tzone = "UTC")),
.Names = "Study_date_time",
row.names = c(NA, -10L),
class = c("tbl_df", "tbl", "data.frame"))
print(df)
# A tibble: 10 x 1
Study_date_time
<dttm>
1 2018-01-05 10:02:28
2 2018-01-05 10:02:28
3 2018-01-03 09:10:58
4 2018-01-03 09:10:58
5 2018-01-05 11:28:52
6 2018-01-05 11:28:52
7 2018-02-01 09:16:29
8 2018-02-01 09:16:29
9 2018-12-03 14:44:06
10 2018-12-03 14:44:06
df%
变异(小时分钟=hms::as.hms(研究日期时间))
#一个tibble:10x2
研究日期时间小时分钟
1 2018-01-05 10:02:28 11:02
2 2018-01-05 10:02:28 11:02
3 2018-01-03 09:10:58 10:10
4 2018-01-03 09:10:58 10:10
5 2018-01-05 11:28:52 12:28
6 2018-01-05 11:28:52 12:28
7 2018-02-01 09:16:29 10:16
8 2018-02-01 09:16:29 10:16
9 2018-12-03 14:44:06 15:44
10 2018-12-03 14:44:06 15:44
可能是由于时区元素被剥离。。。让我猜猜:你生活在地球的UTC+0100区域
您可以使用lubridate软件包中的force_tz()
功能。。但是要小心!!时区总是一个工作场所,所以要小心处理
df %>% mutate(hour_min = hms::as.hms( force_tz( Study_date_time ) ) )
# # A tibble: 10 x 2
# Study_date_time hour_min
# <dttm> <time>
# 1 2018-01-05 10:02:28 10:02
# 2 2018-01-05 10:02:28 10:02
# 3 2018-01-03 09:10:58 09:10
# 4 2018-01-03 09:10:58 09:10
# 5 2018-01-05 11:28:52 11:28
# 6 2018-01-05 11:28:52 11:28
# 7 2018-02-01 09:16:29 09:16
# 8 2018-02-01 09:16:29 09:16
# 9 2018-12-03 14:44:06 14:44
# 10 2018-12-03 14:44:06 14:44
df%>%变异(小时分钟=hms::as.hms(强制时间(研究日期时间)))
##A tible:10 x 2
#研究日期时间小时分钟
#
# 1 2018-01-05 10:02:28 10:02
# 2 2018-01-05 10:02:28 10:02
# 3 2018-01-03 09:10:58 09:10
# 4 2018-01-03 09:10:58 09:10
# 5 2018-01-05 11:28:52 11:28
# 6 2018-01-05 11:28:52 11:28
# 7 2018-02-01 09:16:29 09:16
# 8 2018-02-01 09:16:29 09:16
# 9 2018-12-03 14:44:06 14:44
# 10 2018-12-03 14:44:06 14:44
我同意这可能是一个tz
问题(也许你可以只做hms::as.hms(研究日期时间,tz='UTC'))
,然后它就会消失),但是我认为你也可以不做任何打包工作,例如:
df %>%
mutate(hour_min = format(Study_date_time, "%H:%M"))
@阿格诺:谢谢!我认为这种格式行不通。我需要每小时/分钟制作一个过滤器,时间在周五8点到14点30分之间。我会晚些时候回来,可能是周一,我现在没有电脑。@arg0naut,我在约会时使用了tz
选项。但它不会被带到使用lubridate创建的新变量中。@arg0naut:我问了一个新问题@Wimpel。非常感谢。是的,我住在UTC+1时区。@Wimpel:我问了一个新问题
df %>%
mutate(hour_min = format(Study_date_time, "%H:%M"))