nuwanma
nuwanma

Reputation: 81

Subsetting data table by date range

I've got a data set that I want to select specific time range. I have used following r code but end up with error. Anyone can tell what's wrong with this codes?

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

Error in between(x, y[[1L]], y[[2L]], incbounds = TRUE) : object 'Date' not found

      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

Upvotes: 1

Views: 249

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388837

If you have data.table, you can use as.IDate with %between% as

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
#...

You can also do this in base 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"), ]

Or with lubridate and dplyr without changing the original format of dates

library(dplyr)
library(lubridate)

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

data

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))

Upvotes: 4

Related Questions