34

I have a large dataset in data.table that I'd like to subset by a date range. My data set looks like this:

testset <- data.table(date=as.Date(c("2013-07-02","2013-08-03","2013-09-04",
                                     "2013-10-05","2013-11-06")), 
                      yr = c(2013,2013,2013,2013,2013), 
                      mo = c(07,08,09,10,11),
                      da = c(02,03,04,05,06), 
                      plant = LETTERS[1:5], 
                      product = as.factor(letters[26:22]), 
                      rating = runif(25))

I'd like to be able to choose a date range directly from the as.Date column without using the yr, mo, or da columns. Currently, I'm subsetting by mo and it's extremely clunky at times, especially when years switch over. A more elegant method of doing this would make my life infinitely easier.

Thanks in advance!

black_sheep07
  • 2,308
  • 3
  • 26
  • 40

3 Answers3

41

Why not:

testset[date>="2013-08-02" & date<="2013-11-01"]
Troy
  • 8,581
  • 29
  • 32
23

See also:

?`%between%`

Works like this:

testset[date %between% c("2013-08-02", "2013-11-01")]
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75
  • good point @scottyaz - To clarify to others %between% is part of data.table package. see ?between when package loaded. So if you are going DT route, then it is not too big a sin. – micstr Jul 22 '15 at 11:20
8

You mentioned that you are subsetting, but its not clear whether you are using the subset fn in R.

Type ?subset into the R console to see the details of the subset() function in R which 'returns a subset of vectors, matrices or data frames which meet conditions'. Then use part of the method that Troy posted above to choose the date range

thisYear <- subset(testset, date > "2015-01-01" & date < "2015-12-31")
typeofgraphic
  • 312
  • 4
  • 11