简体   繁体   English

DPLYR 按最大日期和最大日期过滤 - R 中的 7

[英]DPLYR filter by max date and max date - 7 in R

In R,在 R 中,

%>%
  filter(SpecimenDate >= as.Date("2021-10-25") & SpecimenDate <= as.Date("2021-10-31"))

Provides a manual filter and提供手动过滤器和

%>%
  filter(date == max(date))

Filters to the most recent date of data within a table only.仅过滤到表中最近日期的数据。

Can anyone advise please how to create a filter that sorts between max(date)) and max(date))-7 (ie all values within most recent 7 days)?任何人都可以建议如何创建一个在 max(date)) 和 max(date))-7 之间排序的过滤器(即最近 7 天内的所有值)?

I've attempted the below which leads to Error: Problem with filter() input ..1 .我尝试了以下导致 Error: Problem with filter() input ..1尝试。 i Input ..1 is date >= max(date) & date <= max(date - 7) . i 输入..1date >= max(date) & date <= max(date - 7) x non-numeric argument to binary operator x 二元运算符的非数字参数

filter(date >= max(date) & date <= max(date-7)

Thank you.谢谢你。

To filter for the data between the most recent day as well as the 7 days' before it, you only need one condition:要过滤最近一天及其前 7 天之间的数据,您只需要一个条件:

filter(date >= (max(date) - 7))


Just so there's a reproducible example (that anyone can run), this demonstrates the same concept, but using the inbuilt iris dataset只是有一个可重复的示例(任何人都可以运行),这演示了相同的概念,但使用内置的iris数据集

iris %>% 
  filter(Petal.Length < (max(Petal.Length) - 2))

Does this work:这是否有效:

df <- data.frame(c1 = 1:15,
                 c2 = seq.Date(Sys.Date()-14, Sys.Date(), by = 1))
df %>% filter(between(c2, max(c2)-7, max(c2)))
  c1         c2
1  8 2021-10-28
2  9 2021-10-29
3 10 2021-10-30
4 11 2021-10-31
5 12 2021-11-01
6 13 2021-11-02
7 14 2021-11-03
8 15 2021-11-04

You should check str of your data, and please provide reproducible example .您应该检查数据的str ,并请提供可重现的示例

dummy <- data.frame(date = seq.Date(as.Date("2020-01-01"), as.Date("2020-01-15"), by = "day"))

If it's not Date format, It will give that error.如果它不是Date格式,它将给出该错误。

dummy %>%
  mutate(date = as.character(date))%>%
  filter(date >= max(date) & date <= max(date-7))
Error: Problem with `filter()` input `..1`.
i Input `..1` is `date >= max(date) & date <= max(date - 7)`.
x non-numeric argument to binary operator

As your condition is pretty weird, that因为你的情况很奇怪,那

dummy %>%
  mutate(date = as.Date(date))%>%
  filter(date >= max(date) & date <= max(date-7))
         

this will return empty dataframe, but will not reproduce your error.这将返回空数据帧,但不会重现您的错误。

Here's one way to achieve what you are after:这是实现您所追求的目标的一种方法:

library(tidyverse)

dates <- tibble(
  date = seq(as.Date("2021-10-01"), by = "day", length.out = 20))

dates %>% 
  filter(date <= max(date) & date >= max(date) - 7)
#> # A tibble: 8 × 1
#>   date      
#>   <date>    
#> 1 2021-10-13
#> 2 2021-10-14
#> 3 2021-10-15
#> 4 2021-10-16
#> 5 2021-10-17
#> 6 2021-10-18
#> 7 2021-10-19
#> 8 2021-10-20

Created on 2021-11-04 by the reprex package (v2.0.1)reprex 包(v2.0.1) 于 2021 年 11 月 4 日创建

I would use an interval condition.我会使用间隔条件。

    data %>% filter(date %in% (max(date)-7):max(date))

This should work!这应该有效!

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

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