0

I have a tibble with the first column filled with dates. I want to create a smaller tibble with just the rows corresponding with mondays.

I already figured out that I can get the indexes with:

which(wday(df_KBS$Date)==2)

So I thought about looping these indexes over the tibble...but I guess there must be a much easier way.

Example tibble:

library("lubridate")

#create 200 days daterange
posixct.in <- parse_date_time(x = Sys.Date(), orders = "ymd")

posixct.seq <- posixct.in %m+% days(x = seq.int(from = 0, to = 199, by = 1))

#create tibble with date column and random data
df <- tibble(date = posixct.seq,
             group = rep(LETTERS[3:4], each = 100),
             x = runif(n = 200, min = 10, max = 15),
             y = runif(n = 200, min = 100, max = 150))

#find index mondays
which(wday(df$date)==2)
Phil
  • 7,287
  • 3
  • 36
  • 66
H. berg
  • 471
  • 1
  • 3
  • 11
  • 1
    You can do `dplyr::filter(df, wday(date) == 1)` – Phil Jan 15 '22 at 23:09
  • Wauw I tried this yesterday but somehow didnt work..now it did! ...... and if I only want to select date and x? I tried it via: select(date, x) %>% filter(df, wday(date) ==1) but didnt work – H. berg Jan 16 '22 at 10:01
  • Try `df %>% select(date, x) %>% filter(wday(date) == 1)` – Phil Jan 16 '22 at 16:51

0 Answers0