0

I have a dataframe like the one given by:

x <- c(1:6)
y <- c("06/01/13 16:00:00",
       "06/01/13 16:00:00", 
       "06/03/13 20:00:00",
       "06/03/13 20:00:00",
       "06/07/13 20:00:00", 
       "06/08/13 20:00:00")
dfrm <- data.frame(x,y)
dfrm
   x             y
  1 06/01/13 16:00:00
  2 06/01/13 16:00:00
  3 06/03/13 20:00:00
  4 06/03/13 20:00:00
  5 06/07/13 20:00:00
  6 06/08/13 20:00:00

I want to make y a chron object:

dfrm$y <- as.chron(dfrm$y, "%m/%d/%y %H:%M")

Then I have a vector of dates:

intensives <- c("06/01/13", "06/07/13")

Then I want to subset the data frame "dfrm" by the dates in the "intensives" vector. What I would do it would something like:

subset(dfrm, y==dates(intensives))

or

subset(dfrm, y %in% dates(intensives))

but both give me a null result.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Giulia
  • 279
  • 1
  • 4
  • 14

2 Answers2

2

Note:In most person's setups where stringAsFactors=TRUE that conversion to chron would have failed. They would need to do this:

dfrm$y <- as.chron(as.character(dfrm$y), "%m/%d/%y %H:%M")

date-objects are not chron-objects, but chron objects can be coerced with the dates function

 subset(dfrm, dates(y) %in% dates(intensives))
  x                   y
1 1 (06/01/13 16:00:00)
2 2 (06/01/13 16:00:00)
5 5 (06/07/13 20:00:00)
IRTFM
  • 258,963
  • 21
  • 364
  • 487
1

That's because you're comparing datetimes to dates.

Do subset(dfrm, dates(y) %in% dates(intensives)) instead.

You first subset using == will never work, regardless of data type.

Señor O
  • 17,049
  • 2
  • 45
  • 47