0

I am trying to subset dates, such as here:

  df <- data.frame(
    paste0("ID", rep(1:5)),  
    c("8/1/2019", "9/1/2019", "10/1/2019", "11/1/2019", "12/1/2019"))

  names(df) <- c("ID", "dates")

  df[as.POSIXct(strptime(df$dates, "%m/%d/%Y")) <
     as.POSIXct("10/1/2019", "%m/%d/%Y"), ]

But I get the following warnings:

    1: In strptime(xx, f, tz = tz) : unknown timezone '%m/%d/%Y'
    2: In as.POSIXct.POSIXlt(x) : unknown timezone '%m/%d/%Y'

I ran these commands regularly in the past and not had this issue till now. It started on December 31st, so possibly the new year/new decade is the cause, but I am not certain.

I tried setting the timezone manually as recommended in this post, I also updated from v. 3.5.3 to 3.6.2. Neither fixed the issue.

Any insight into what's going on here?

votmoyd
  • 65
  • 8
  • 1
    Have you tried this? `df[as.POSIXct(strptime(df$dates, "%m/%d/%Y"), tz = "America/Chicago") < as.POSIXct("10/1/2019", "%m/%d/%Y", tz = "America/Chicago"), ]` – TheSciGuy Jan 02 '20 at 20:27
  • 3
    Avoid the whole timezone concern and just use `as.Date()` – Dave2e Jan 02 '20 at 20:31
  • @TheSciGuy I had tried df[as.POSIXct(strptime(df$dates, "%m/%d/%Y", tz = "America/Chicago")) but the argument "tz =" was within the parentheses so it didn't work. – votmoyd Jan 02 '20 at 21:31
  • @Dave2e, that worked as well, thanks! – votmoyd Jan 02 '20 at 21:34

1 Answers1

2

In your last line, you have

as.POSIXct("10/1/2019", "%m/%d/%Y")

The arguments to as.POSIXct are (x, tz, ...). To specify a format, you need to name it, i.e.

df[as.POSIXct(strptime(df$dates, "%m/%d/%Y")) <
   as.POSIXct("10/1/2019", format = "%m/%d/%Y"), ]

or even better,

df[as.POSIXct(df$dates, format = "%m/%d/%Y") <
   as.POSIXct("10/1/2019", format = "%m/%d/%Y"), ]
user2554330
  • 37,248
  • 4
  • 43
  • 90