0

I love using dplyr; I use it for everything. But, the problem I'm experimenting today is the following:

I'm trying to simply filter all rows fromm my opps table where opp_date is from today. So, when I use filter(opps, as.Date(opp_date) == Sys.Date()) it's bringing today's data but also yesterday's too, from 19:00:00 onwards.

To clarify any possible problem:

  • opp_date field is POSIXct class
  • Sys.Date() returns correctly my current date and time (just to check, Sys.time() brings the correct time and date: "2017-07-21 10:06:04 COT")

Any idea here? Thanks to the community for all the great inputs :)

Bernardo
  • 461
  • 7
  • 20
  • `as.Date.POSIXct` has a bad default time zone, as I learned recently. You might want to check its doc. – Frank Jul 21 '17 at 15:16
  • 1
    And it's funny because if I export and import a CSV with the data, I use the same `as.Date()` function and it works!! – Bernardo Jul 21 '17 at 15:44
  • Check that the time zone used to calculate `opp_date` is the same as the one your system is using. If opp_date was recorded in UTC-10, that would account for your five hour offset I think – Mako212 Jul 21 '17 at 16:07
  • @Mako212 thanks for your reply but actually both, `Sys.Date()` and `opp_date` are equally "GMT-5". As I write in the past comment, when I export and then import the dates (as CSV), `dplyr` works perfectly when bringing today's rows. – Bernardo Jul 24 '17 at 13:44

2 Answers2

1

The issue must be due to different time-zones; by default, R uses your system's local time-zone.

Try to explicitly set the environment variable as follows:

Sys.setenv(TZ='UTC')
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Pri
  • 11
  • 3
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 01 '21 at 08:27
0

Cannot add a comment due to low repputation, so posting this as an answer.

As I got to know recently, R dates are a formatting nightmare, especially through the base functions. Checkout lubridate package. You may want to convert your date column using dmy_hms function. It's easy and vectors are supported by default. Try it and let me know if the problem persists.

And please always try to provide sample data. Otherwise people cannot reproduce your problems.

Arani
  • 753
  • 1
  • 9
  • 23