Filter for first day in the year? How to do with filter and lubridate?

My df (first few rows...):

symbol date open high low close volume adjusted

1 ^GSPC 1930-01-02 21.2 21.2 21.2 21.2 0 21.2
2 ^GSPC 1930-01-03 21.2 21.2 21.2 21.2 0 21.2
3 ^GSPC 1930-01-06 21.5 21.5 21.5 21.5 0 21.5
4 ^GSPC 1930-01-07 21.3 21.3 21.3 21.3 0 21.3
5 ^GSPC 1930-01-08 21.3 21.3 21.3 21.3 0 21.3
6 ^GSPC 1930-01-09 21.6 21.6 21.6 21.6 0 21.6
7 ^GSPC 1930-01-10 21.5 21.5 21.5 21.5 0 21.5
8 ^GSPC 1930-01-13 21.5 21.5 21.5 21.5 0 21.5
9 ^GSPC 1930-01-14 21.6 21.6 21.6 21.6 0 21.6
10 ^GSPC 1930-01-15 21.6 21.6 21.6 21.6 0 21.6

My code:

df <- tq_get("^GSPC", from='1930-01-01', to='2022-12-31')
df %>% filter(yday(date) == 1)

==> does not work ... I get the following message: # A tibble: 0 x 8
# … with 8 variables: symbol , date , open , high , low , close ,
# volume , adjusted
# :information_source: Use colnames() to see all variable names

This code works for filtering:

df %>% filter(month(date) == 1, day(date) == 2)
When I change to day(date) == 1 it does not work.

I would like to just filter for the first day of each year.

Thank you for your help.

Can this be useful?

library(tidyverse)
library(lubridate)

df <- tibble(name = c("A", "B", "C", "D"),
            date = c("02-11-2022", "01-01-2021", "03-06-2020", "01-01-2019"))
# A tibble: 4 × 2
  name  date      
  <chr> <chr>     
1 A     02-11-2022
2 B     01-01-2021
3 C     03-06-2020
4 D     01-01-2019

code

df %>% 
  mutate(date = dmy(date),
         monthday = paste(month(date), day(date), sep = "-")) %>% 
  filter(monthday == "1-1")

result

# A tibble: 2 × 3
  name  date       monthday
  <chr> <date>     <chr>   
1 B     2021-01-01 1-1     
2 D     2019-01-01 1-1   
1 Like

Still not working. Nonetheless thank you for your answer.

I get this message:

**# A tibble: 0 x 4** **# … with 4 variables: symbol <chr>, date <date>, close <dbl>, monthday <chr>** **# ℹ Use `colnames()` to see all variable names** **Warning message:** **Problem while computing `date = dmy(date)`.** **ℹ All formats failed to parse. No formats found.**

df %>% filter(month(date) == 1, day(date) == 2)

every number works except for 1, why?

can you copy and paste the output: dput(head(YOURDF, 10))?

structure(list(symbol = c("^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC"), date = structure(c(-14609, -14608, -14605, -14604, -14603, -14602, -14601, -14598, -14597, -14596), class = "Date"), open = c(21.18, 21.23, 21.5, 21.309999, 21.290001, 21.620001, 21.549999, 21.52, 21.559999, 21.65), high = c(21.18, 21.23, 21.5, 21.309999, 21.290001, 21.620001, 21.549999, 21.52, 21.559999, 21.65), low = c(21.18, 21.23, 21.5, 21.309999, 21.290001, 21.620001, 21.549999, 21.52, 21.559999, 21.65), close = c(21.18, 21.23, 21.5, 21.309999, 21.290001, 21.620001, 21.549999, 21.52, 21.559999, 21.65), volume = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), adjusted = c(21.18, 21.23, 21.5, 21.309999, 21.290001, 21.620001, 21.549999, 21.52, 21.559999, 21.65)), row.names = c(NA, -10L ), class = c("tbl_df", "tbl", "data.frame"))

this is the df:

library(tidyverse)
library(tidyquant)
library(lubridate)

df <- tq_get("^GSPC", from='1930-01-01', to='2022-12-31')

df <- structure(list(symbol = c("^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC", "^GSPC"), date = structure(c(-14609, -14608, -14605, -14604, -14603, -14602, -14601, -14598, -14597, -14596), class = "Date"), open = c(21.18, 21.23, 21.5, 21.309999, 21.290001, 21.620001, 21.549999, 21.52, 21.559999, 21.65), high = c(21.18, 21.23, 21.5, 21.309999, 21.290001, 21.620001, 21.549999, 21.52, 21.559999, 21.65), low = c(21.18, 21.23, 21.5, 21.309999, 21.290001, 21.620001, 21.549999, 21.52, 21.559999, 21.65), close = c(21.18, 21.23, 21.5, 21.309999, 21.290001, 21.620001, 21.549999, 21.52, 21.559999, 21.65), volume = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), adjusted = c(21.18, 21.23, 21.5, 21.309999, 21.290001, 21.620001, 21.549999, 21.52, 21.559999, 21.65)), row.names = c(NA, -10L ), class = c("tbl_df", "tbl", "data.frame"))

library(lubridate)
library(tidyverse)

Code

df %>% 
  mutate(date = ymd(date),
         monthday = paste(month(date), day(date), sep = "-")) %>% 
  filter(monthday == "1-1")

This table doesn't contain january 01, so the table is empty.
Anyway the code works as expected: if i check for january, 07 using filter(monthday == "1-7") the result is

# A tibble: 1 × 9
  symbol date        open  high   low close volume adjusted monthday
  <chr>  <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl> <chr>   
1 ^GSPC  1930-01-07  21.3  21.3  21.3  21.3      0     21.3 1-7

:man_facepalming: sorry, been looking for the 1st of Jan; please excuse me for using your time, thank you very much for your help; really sorry

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.