简体   繁体   English

使用 dplyr 在 R 中排除 28 个不同月份的子集数据框

[英]Subset Data Frame to Exclude 28 Different Months in R Using dplyr

I have a data frame consisting of monthly volumes beginning 2004-01-01 and ending 2019-12-01.我有一个数据框,其中包含从 2004-01-01 开始到 2019-12-01 结束的月交易量。 I need to apply a filter that will delete rows that equal certain dates.我需要应用一个过滤器来删除等于特定日期的行。 My problem is that there's 28 dates I need to filter out and they arent consecutive.我的问题是我需要过滤掉 28 个日期,而且它们不是连续的。 What I have right now works, but isn't efficient.我现在所拥有的有效,但效率不高。 I am using dplyr's filter function.我正在使用 dplyr 的过滤功能。

I currently have 28 variables, d1-d28, which are the dates that I would like filtered out and then I use我目前有 28 个变量,d1-d28,这是我想过滤掉的日期,然后我使用

df<-data%>%dplyr::filter(Date!=d1 & Date!=d2 & Date!=d3 .......Date!=d28)

I would like to put the dates of interest, the d1-d28, into a data.frame and just reference the data.frame in my filter code.我想将感兴趣的日期 d1-d28 放入 data.frame 中,然后在我的过滤器代码中引用 data.frame。

I've tried:我试过了:

df<-data%>%dplyr::filter(!Date %in% DateFilter) 

Where DateFilter is a data.frame with 1 column and 28 rows of the dates I want filtered, but I get an an error where it says the length of the objects don't match.其中 DateFilter 是一个 data.frame,其中包含 1 列和 28 行我想要过滤的日期,但我收到一个错误,其中说对象的长度不匹配。

Is there any way I can do this with dplyr?有什么办法可以用 dplyr 做到这一点吗?

Here, we may use filter_at在这里,我们可以使用filter_at

library(dplyr)
data %>%
      filter_at(vars(matches('^d\\d+$')), all_vars(Date != .))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM