按日期范围子集数据表

时间:2019-10-10 03:40:24

标签: r

我有一个要选择特定时间范围的数据集。我使用了以下r代码,但最终出现错误。任何人都可以说出此代码有什么问题吗?

dat[Date%between% c("01/07/2016","30/06/2019")]
  

介于(x,y [[1L]],y [[2L]],inbounds = TRUE)之间的错误:     找不到对象“日期”

      Date    Chla
22/12/2015  0.0084
19/04/2016  0.0036
11/08/2016  0.0019
2/12/2016   0.0013
2/03/2017   0.003
6/06/2017   0.003
4/09/2017   0.005
6/12/2017   0
1/03/2018   0.02
12/06/2018  0.09
3/07/2018   0.04
2/08/2018   0.026
11/09/2018  0.02
3/10/2018   0.02
13/11/2018  0.01
14/12/2018  0
18/01/2019  0
22/02/2019  0.05
21/03/2019  0
17/04/2019  0
13/05/2019  0.03
18/06/2019  0
19/07/2019  0.002
19/08/2019  0.0018
12/04/2012  0.012

1 个答案:

答案 0 :(得分:3)

如果您有data.table,则可以将as.IDate%between%一起使用

library(data.table)
setDT(df)

df[as.IDate(Date, "%d/%m/%Y") %between% as.IDate(c("2016-07-01","2019-06-30"))]

#        Date   Chla
# 1: 11/08/2016 0.0019
# 2:  2/12/2016 0.0013
# 3:  2/03/2017 0.0030
# 4:  6/06/2017 0.0030
# 5:  4/09/2017 0.0050
# 6:  6/12/2017 0.0000
# 7:  1/03/2018 0.0200
#...

您也可以在基本R中执行此操作

df$Date <- as.Date(df$Date, "%d/%m/%Y")
df[df$Date >= as.Date("2016-07-01") & df$Date <= as.Date("2019-06-30"), ]

或者使用lubridatedplyr而不更改日期的原始格式

library(dplyr)
library(lubridate)

df %>% filter(between(dmy(Date), date("2016-07-01"), date("2019-06-30")))

数据

df <- structure(list(Date = structure(c(20L, 12L, 2L, 17L, 15L, 24L, 
23L, 25L, 1L, 5L, 21L, 16L, 3L, 22L, 7L, 8L, 10L, 19L, 18L, 9L, 
6L, 11L, 13L, 14L, 4L), .Label = c("1/03/2018", "11/08/2016", 
"11/09/2018", "12/04/2012", "12/06/2018", "13/05/2019", "13/11/2018", 
"14/12/2018", "17/04/2019", "18/01/2019", "18/06/2019", "19/04/2016", 
"19/07/2019", "19/08/2019", "2/03/2017", "2/08/2018", "2/12/2016", 
"21/03/2019", "22/02/2019", "22/12/2015", "3/07/2018", "3/10/2018", 
"4/09/2017", "6/06/2017", "6/12/2017"), class = "factor"), Chla = c(0.0084, 
0.0036, 0.0019, 0.0013, 0.003, 0.003, 0.005, 0, 0.02, 0.09, 0.04, 
0.026, 0.02, 0.02, 0.01, 0, 0, 0.05, 0, 0, 0.03, 0, 0.002, 0.0018, 
0.012)), class = "data.frame", row.names = c(NA, -25L))