0

It's not difficult to do that (some ways that I know of below). Just being curious if there is already a function out there that generates such a sequence very easily, ideally with a rather common package or even base R.

## just for printing purpose
headtail <- function(x){
  cat(as.character(head(x, 3)), "...", as.character(tail(x, 3)))
}
## is ok, but lots of typing
x <- seq(as.Date("2021-01-01"), as.Date("2021-12-31"), by = "day")
headtail(x)
#> 2021-01-01 2021-01-02 2021-01-03 ... 2021-12-29 2021-12-30 2021-12-31

## shorter, but requires a priori knowledge of length of this year
x <- seq(as.Date("2021-01-01"), length = 365, by = "day")
headtail(x)
#> 2021-01-01 2021-01-02 2021-01-03 ... 2021-12-29 2021-12-30 2021-12-31
## or 
x <- as.Date(0:364, origin = "2021-01-01")
headtail(x)
#> 2021-01-01 2021-01-02 2021-01-03 ... 2021-12-29 2021-12-30 2021-12-31

## I can of course make my own function
ydays <- function(year){
  seq(as.Date(paste0(as.character(year), "-01-01")), as.Date(paste0(as.character(year), "-12-31")), by = "day")
}
x <- ydays(2021)
headtail(x)
#> 2021-01-01 2021-01-02 2021-01-03 ... 2021-12-29 2021-12-30 2021-12-31
tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Related: [Create a vector of all dates in a given year](https://stackoverflow.com/questions/7176870/create-a-vector-of-all-dates-in-a-given-year), also looking for a "simple R idiom". – Henrik Feb 11 '23 at 13:43
  • Thanks @Henrik :) I like the term "simple R idiom". I guess from the given answers, the answer to my question is: "There isn't"... – tjebo Feb 11 '23 at 13:54
  • Your first and last solutions look fine to me. You don't need `as.character()` in the `ydays` function though. – user2554330 Feb 11 '23 at 13:55
  • 1
    I reckon there are "simple" functions in `lubridate` or `clock`, but of course there are a lot of things going on under the hood. So I bet your `ydays` may be as nice. – Henrik Feb 11 '23 at 13:57

0 Answers0