# 每年在R中按日期子集

``````site year date
A    2019 2019-08-08
A    2019 2019-08-18
A    2019 2019-10-14
A    2019 2019-10-27
B    2019 2019-08-07
B    2019 2019-08-19
B    2019 2019-10-15
B    2019 2019-10-20
A    2020 2020-08-08
A    2020 2020-08-18
A    2020 2020-10-14
A    2020 2020-10-27
B    2020 2020-08-07
B    2020 2020-08-19
B    2020 2020-10-15
B    2020 2020-10-20
``````

=&lt；10月15日

## 推荐答案

``````library(dplyr)
library(lubridate)
df1 %>%
mutate(date = ymd(date)) %>%
filter(between(yday(date), 228, 289))
``````

-输出

`````` site year       date
1    A 2019 2019-08-18
2    A 2019 2019-10-14
3    B 2019 2019-08-19
4    B 2019 2019-10-15
5    A 2020 2020-08-18
6    A 2020 2020-10-14
7    B 2020 2020-08-19
8    B 2020 2020-10-15
``````

### 数据

``````df1 <- structure(list(site = c("A", "A", "A", "A", "B", "B", "B", "B",
"A", "A", "A", "A", "B", "B", "B", "B"), year = c(2019L, 2019L,
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2020L, 2020L, 2020L,
2020L, 2020L, 2020L, 2020L, 2020L), date = c("2019-08-08", "2019-08-18",
"2019-10-14", "2019-10-27", "2019-08-07", "2019-08-19", "2019-10-15",
"2019-10-20", "2020-08-08", "2020-08-18", "2020-10-14", "2020-10-27",
"2020-08-07", "2020-08-19", "2020-10-15", "2020-10-20")),
class = "数据.frame", row.names = c(NA,
-16L))
``````