在多个日期范围之间过滤

如何解决在多个日期范围之间过滤

我有一个很大的数据框,在两周内每秒进行四次测量。因此数据框很大。
我还有两个带有开始日期和结束日期的向量,它们定义了数据帧内的某些时间范围,必须将其过滤掉。
我要做的是提取开始日期和结束日期之间的数据。
我的数据看起来像这样。

library(lubridate)
library(dplyr)

df <- data.frame(datetime = seq(ymd_hms("2020/01/01 00:00:00"),by = "sec",length.out = 3600),var = rnorm(3600))

我的开始/结束向量看起来像这样。在这里,我仅添加了两个开始/结束组合。但是实际向量包含更多的值。

start = c(ymd_hms("2020/01/01 00:1:00"),ymd_hms("2020/01/01 00:30:00"))
end = c(ymd_hms("2020/01/01 00:1:04"),ymd_hms("2020/01/01 00:30:04"))

我试图使用来过滤它

filtered <- df %>%
  filter(datetime >= start & datetime <= end)

head(filtered)

             datetime        var
1 2020-01-01 00:01:00 -0.2245330
2 2020-01-01 00:01:02  0.5926424
3 2020-01-01 00:01:04 -0.3824533
4 2020-01-01 00:30:01 -0.7202059
5 2020-01-01 00:30:03 -0.5775794

但是它似乎降低了数据采样率,因为过滤后的数据帧在第一个时间间隔内只有三个测量值,而不是预期的五个值。

如果仅针对第一个开始日期和结束日期进行过滤,则会得到5个值。

filtered2 <- df %>%
  filter(datetime >= start[1] & datetime <= end[1])

head(filtered2)

             datetime         var
1 2020-01-01 00:01:00 -0.22453305
2 2020-01-01 00:01:01  1.13452854
3 2020-01-01 00:01:02  0.59264239
4 2020-01-01 00:01:03 -0.03700048
5 2020-01-01 00:01:04 -0.38245332

我遇到的困难是:
为什么第一次过滤有效,但不返回预期的完整日期范围?
以及如何过滤完整的数据?

我已经尝试过filter(between(datetime,start,end)。这给了我预期的结果,但仅在第一个日期范围内。看来dplyr::between不接受任何绒毛。

非常欢迎任何帮助。

更新
@ekoam正确指出data.table::between也可以工作。但是作为dplyr::between,它不喜欢矢量。

解决方法

正确的语法是

df %>% filter(dplyr::between(datetime,start[[1L]],end[[1L]]) | dplyr::between(datetime,start[[2L]],end[[2L]]))

更新

我做了以下检查:

res1 <- df %>% filter(data.table::between(datetime,end[[1L]]) | data.table::between(datetime,end[[2L]]))

res2 <- df %>% filter(dplyr::between(datetime,end[[2L]]))

all(res1 == res2)

输出

> all(res1 == res2)
[1] TRUE

因此,如果您还使用dplyr 1.0.2data.table 1.13.0,则可以使用dplyr::betweendata.table::between来完成任务。

,

从这个有关如何Efficient way to filter one data frame by ranges in another的问题中得到一些启发,我提出了以下解决方案。

一个非常大的数据集非常慢:
它使用上面提供的我的数据并使用rowwise()

filtered3 <- df %>% 
  rowwise() %>%
  filter(any(datetime >= start & datetime <= end))

正如我提到的,在我的数据中有超过300万行,这非常慢。

另一个选项,也是来自上面链接的答案,包括使用具有inrange功能的data.table包。这一个工作更快。

library(data.table)
range <- data.table(start = start,end = end)
filtered4 <- setDT(df)[datetime %inrange% range]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-