Reputation: 2945
I am trying to filter my tibble
for a specific date. If I try >=
that date & <=
that same date, the code results in 0 rows being returned. I have identified that it is the <=
that returns FALSE
for rows that I believe should be TRUE
. Below is a working example. I am only interesting in tidyverse
and lubridate
related responses - please do not post answers using Base R or other packages (I know how to solve the problem in these other ways.
Data
library(dplyr)
library(lubridate)
d <- structure(list(Date = structure(c(1572679159, 1572703322, 1572710363, 1572722578,
1572727765, 1572728767, 1572730032),
class = c("POSIXct", "POSIXt"),
tzone = "UTC", label = c(EndDate = "EndDate")),
ID = 1:7), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -7L))
When I try the following, it returns a tibble
with 0 rows:
d %>%
filter(Date >= as_date("2019-11-02"),
Date <= as_date("2019-11-02"))
When I try this, however, it works. What am I missing? The problem is that, I would like Date <= as_date("2019-11-03")
to return TRUE
for all '2019-11-03' submissions (I actually have those in my dataset), but it does not.
d %>%
filter(Date >= as_date("2019-11-02"),
Date <= as_date("2019-11-03"))
Upvotes: 3
Views: 5850
Reputation: 522
%in% mean to determine whether a particular value is included in a vector operator. So you can use %in%
.
d %>%
filter(Date >= as.Date("2019-11-02") %in%
(Date <= as_date("2019-11-03")))
# A tibble: 7 x 2
Date ID
<dttm> <int>
1 2019-11-02 07:19:19 1
2 2019-11-02 14:02:02 2
3 2019-11-02 15:59:23 3
4 2019-11-02 19:22:58 4
5 2019-11-02 20:49:25 5
6 2019-11-02 21:06:07 6
7 2019-11-02 21:27:12 7
Upvotes: -2
Reputation: 388982
You need to convert the datetime object to date class and then compare. Also use &
to include multiple conditions to check.
library(dplyr)
library(lubridate)
d %>%
filter(as_date(Date) >= as_date("2019-11-02") &
as_date(Date) <= as_date("2019-11-02"))
# Date ID
# <dttm> <int>
#1 2019-11-02 07:19:19 1
#2 2019-11-02 14:02:02 2
#3 2019-11-02 15:59:23 3
#4 2019-11-02 19:22:58 4
#5 2019-11-02 20:49:25 5
#6 2019-11-02 21:06:07 6
#7 2019-11-02 21:27:12 7
Moreover, we can also use between
d %>%
filter(between(as_date(Date), as_date("2019-11-02"), as_date("2019-11-02")))
Upvotes: 6