如何将 lubricate 的 %within% 与 dplyr 结合使用?
How to use lubricate's %within% in conjunction with dplyr?
我正在尝试过滤存在于“非活动工作班次”中的传感器数据。这意味着我想将数据帧的每个 DateTime 元素与间隔列表进行比较,以检查在这种转变期间是否记录了传感器数据。
我正在像这样构建所有时间间隔:
INAS_intervals <- raw_sensor_data %>%
filter(error_code == "INAS") %>%
summarise(inactive = interval(ymd_hms(from), ymd_hms(to)))
导致:
inactive
<S4: Interval>
2019-08-03 06:00:00 UTC--2019-08-03 12:30:00 UTC
2019-08-03 12:30:00 UTC--2019-08-04 06:00:00 UTC
2019-08-04 06:00:00 UTC--2019-08-04 12:30:00 UTC
像这样构建过滤器(损坏):
saw %>%
filter(any(dateTime %within% INAS_intervals)) %>%
mutate(diffi = abs(machSpeed - curMachSpeed))
(...)
> longer object length is not a multiple of shorter object length...
为什么不能像这样对列 'dateTime'(类型:POSIXct)使用 %within%?
旁注:
any(saw[1,1] %within% INAS_intervals)
>Error in saw[1, 1] %within% INAS_intervals :
No %within% method with signature a = tbl_df, b = tbl_df
但这很好用:
any(saw[[1,1]] %within% INAS_intervals)
[1] FALSE
在这种情况下我必须使用 lapply()、sapply() 或 apply() 吗?我对使用这种技术非常缺乏经验。在此先感谢您的帮助!
这里需要一个rowwise
library(dplyr)
saw %>%
rowwise %>%
filter(dateTime %within% INAS_intervals) %>%
ungroup %>%
mutate(diffi = abs(machSpeed - curMachSpeed))
我正在尝试过滤存在于“非活动工作班次”中的传感器数据。这意味着我想将数据帧的每个 DateTime 元素与间隔列表进行比较,以检查在这种转变期间是否记录了传感器数据。
我正在像这样构建所有时间间隔:
INAS_intervals <- raw_sensor_data %>%
filter(error_code == "INAS") %>%
summarise(inactive = interval(ymd_hms(from), ymd_hms(to)))
导致:
inactive
<S4: Interval>
2019-08-03 06:00:00 UTC--2019-08-03 12:30:00 UTC
2019-08-03 12:30:00 UTC--2019-08-04 06:00:00 UTC
2019-08-04 06:00:00 UTC--2019-08-04 12:30:00 UTC
像这样构建过滤器(损坏):
saw %>%
filter(any(dateTime %within% INAS_intervals)) %>%
mutate(diffi = abs(machSpeed - curMachSpeed))
(...)
> longer object length is not a multiple of shorter object length...
为什么不能像这样对列 'dateTime'(类型:POSIXct)使用 %within%?
旁注:
any(saw[1,1] %within% INAS_intervals)
>Error in saw[1, 1] %within% INAS_intervals :
No %within% method with signature a = tbl_df, b = tbl_df
但这很好用:
any(saw[[1,1]] %within% INAS_intervals)
[1] FALSE
在这种情况下我必须使用 lapply()、sapply() 或 apply() 吗?我对使用这种技术非常缺乏经验。在此先感谢您的帮助!
这里需要一个rowwise
library(dplyr)
saw %>%
rowwise %>%
filter(dateTime %within% INAS_intervals) %>%
ungroup %>%
mutate(diffi = abs(machSpeed - curMachSpeed))