The data:
library(tidyverse)
library(tidyquant)
library(lubridate)
df <- tq_get("^GSPC", from='1930-01-01', to='2022-12-31')
I want to filter for the first date of every year.
My code:
df %>% filter(month(date) == 1, day(date) == 1) %>% head(n=Inf)
I get the following message:
A tibble: 0 x 8 … with 8 variables: symbol , date , open , high , low , close , volume , adjusted Use
colnames()
to see all variable names
If I use any other number than 1 in day(date) == 1
the code works.
This code works:
df %>% filter(month(date) == 1, day(date) == 2) %>% head(n=Inf)
Question:
Why is day(date) == 1
not working?
df %>% with(month(date) == 1 & day(date) == 1) %>% any()
> [1] FALSE
This result shows that there isn't any observation for january first in your dataset, thus filter
always returns an empty tibble. The problem is with your data, not the code.
Another way to check it:
df$date %>% paste() %>% grepl("[0-9]{4}-01-01", .) %>% any()
> [1] FALSE