簡體   English   中英

如何使用 dplyr 過濾 r 中的日期?

[英]How do filter dates in r using dplyr?

下面是我的數據集:

df_gather %>% 
  filter(Country.Region %in% c("Italy")) %>%
  arrange(desc(Date)) #%>% 
  # filter(Date %in% c(2020-12-12))

########### output #############

Country.Region Date   Cases_Count
<chr>          <date> <int>

Italy   2020-12-12  1825775     
Italy   2020-12-11  1805873     
Italy   2020-12-10  1787147     
Italy   2020-12-09  1770149     
Italy   2020-12-08  1757394     
Italy   2020-12-07  1742557     
Italy   2020-12-06  1728878     
Italy   2020-12-05  1709991     
Italy   2020-12-04  1688939     
Italy   2020-12-03  1664829     

問題:當我嘗試使用下面的代碼filter它時,我得到0 rows作為回報

df_gather %>% 
  filter(Country.Region %in% c("Italy")) %>%
  arrange(desc(Date)) %>%
  filter(Date %in% c(2020-12-12))

######## output #########
0 rows

還嘗試在日期上加上引號,但即使那樣我也得到相同的結果

df_gather %>% 
  filter(Country.Region %in% c("Italy")) %>%
  arrange(desc(Date)) %>%
  filter(Date %in% c("2020-12-12"))

######## output #########
0 rows

這似乎但無法弄清楚。 即使在過濾中也有不同的處理日期的方法嗎?

應引用日期,最好在類似類型之間進行比較,即使用 as.Date 轉換為Date as.Date

library(dplyr)
df_gather %>% 
   filter(Country.Region %in% c("Italy")) %>%
   arrange(desc(Date)) %>%
   filter(Date %in% as.Date(c("2020-12-12")))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM