1

I'm trying to make a simple subset of a df based on a POSIXct variable, but it doesn't work...

a2001 <- subset(anillas, anillas$data=="21-03-2001")

it gives "0 obs. of 74 variables"

are those variables different in any way I don't know?

Tks.

Ok, so this is part of the str(anillas)

 $ rc          : num  NA NA NA NA NA NA NA NA NA NA ...
 $ ss          : num  NA NA NA NA NA NA NA NA NA NA ...
 $ tg          : num  NA NA NA NA NA NA NA NA NA NA ...
 $ ug          : num  NA NA NA NA NA NA NA NA NA NA ...
 $ data        : POSIXct, format: "2001-03-21" "2001-03-23" ...
Miguel
  • 67
  • 2
  • 9
  • 2
    please add the output of `dput(head(anillas))` to your question. Also, please see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – GSee Nov 20 '12 at 18:27
  • dput showed a very very long list so it will take me a little while to prepare a short one (based on what you suggest)(i'm just begining with R) – Miguel Nov 20 '12 at 18:58
  • is the problem that your date format in `subset(annillas, anillas$data == "21-03-2001")` is different to the date format in `anillas$data`? try `which (anillas$data=="21-03-2001")` to see if anything is returned. If not, try again with `which(anillas$data=="2001-03-21")`. I suspect that `a2001 <- subset(anillas, anillas$data=="2001-03-21")` might do what you want. – ricardo Nov 21 '12 at 10:37
  • in both cases I get `integer(0)`, with the original UK date format and with the US format – Miguel Nov 21 '12 at 17:19

1 Answers1

3

You have to compare with what's comparable. In your attempt you compared a POSIXct object with a character string.
You need first to convert the date you want to subset anillas with to POSIXct:

date1 <- strptime("21-03-2001","%d-%m-%Y")
a2001 <- subset(anillas, data==date1)
plannapus
  • 18,529
  • 4
  • 72
  • 94
  • There's nothing wrong with their data and what they show isn't impossible. If the time is exactly midnight, no time is printed by default. For example: `str(as.POSIXct("2012-11-21",tz="GMT"))`. – Joshua Ulrich Nov 21 '12 at 13:04
  • @JoshuaUlrich fair enough, I didn't know that (never encountered the case before to be honest, and thought it would print `00:00:00`). I tried to reproduce it but in my haste didn't specify `tz="GMT"`, hence the `01:00:00`. – plannapus Nov 21 '12 at 15:15
  • I created date1 and then subset... but a2001 still `0 obs of 74 variables` – Miguel Nov 21 '12 at 17:25
  • Ok, finally I give up, I created a new variable with `year()` of package `lubridate`and everything worked fine. Thanks you all!! – Miguel Nov 21 '12 at 17:26