dplyr过滤器不适用于lubridate [英] dplyr filter not working with lubridate

查看:64
本文介绍了dplyr过滤器不适用于lubridate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解以下代码的行为方式.基本上,过滤器似乎已应用,但随后在后续调用中无法正常工作.

I am having troubles understanding why the code below behaves how it behaves. Basically, the filter seems to be applied but then fails to work in subsequent calls.

> library(dplyr)
> library(lubridate)
> 
> md1 <- data.frame(no = 1:4, time = c("12:30:00", "13:30:00", "14:30:00", "15:30:00"))
> md1$time <- hms(md1$time)
> md1
  no       time
1  1 12H 30M 0S
2  2 13H 30M 0S
3  3 14H 30M 0S
4  4 15H 30M 0S
> md2 <- filter(md1, hour(time)<14)
> md2
  no       time
1  1 12H 30M 0S
2  2 13H 30M 0S
> hour(md2$time)
[1] 12 13 14 15

我希望/希望收到 [1] 12 13 在上次通话中

while I would like / expect to receive [1] 12 13 in the last call

欢迎提出任何建议.在R(Windows 7)的3.3.1和3.4.0版本中,其行为相同.

Any suggestions welcome. It behaves the same in 3.3.1 and 3.4.0 versions of R (Windows 7)

推荐答案

 md2 <- md1 %>%  
 + mutate(time = format(time, format="%H:%M:%S")) %>%
 + filter(time < "14:00:00")

 md2

 no       time
 1  1 12H 30M 0S
 2  2 13H 30M 0S

md2$time
[1] "12H 30M 0S" "13H 30M 0S"

如果只想一个小时

hour(hms(as.character(md2$time)))
[1] 12 13

这篇关于dplyr过滤器不适用于lubridate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆