Hourly Date Sequence In R

11,913

Well, don't you give the answer yourself ;-)

What about:

seq(as.POSIXct("1912-02-24 23:00:00"), as.POSIXct("1912-02-25 08:32:00"), by="hour")

(also works with as.POSIXlt).

Share:
11,913

Related videos on Youtube

Gavin Simpson
Author by

Gavin Simpson

I'm an Assistant Professor of Applied Statistics in the Department of Animal Science, at the Aarhus University, Aarhus, Denmark. I undertake research on environmental problems, including climate change and atmospheric pollution, affecting lakes. I use lake sediments to look back in time at the history of lakes to look at what organisms are present and how the species in the lake have changed through time and how lakes evolve and respond to pollution and perturbations. I'm now applying these approaches to problems in Animal Science at the interaction between the environment and human and animal health.

Updated on September 24, 2022

Comments

  • Gavin Simpson
    Gavin Simpson over 1 year

    I am trying to produce a sequence of dates at hourly intervals. As an example I tried the following:

    > seq(as.Date("1912-02-24 23:00:00"), as.Date("1912-02-25 08:32:00"), by="hour")
    

    which produces an error, yet it works fine for by= year, month, day.

    The documentation for seq.POSIXt {base} states that it should work for hour, sec, min, etc, yet all of these produce an error.

    > ?seq.POSIXt
    

    Thanks in advance.

    • Gavin Simpson
      Gavin Simpson almost 12 years
      But you have objects of class "Date" not "POSIXt"! seq.Date() would then apply and as this relates to just dates, where the day is the minimum unit of time, how do you expect it to increment the Dates by hours?
  • DarrenRhodes
    DarrenRhodes over 2 years
    what about 4 hourly intervals?
  • gui11aume
    gui11aume over 2 years
    @DarrenRhodes the unit of by is the second. You can do by=3600 * 4, or replace 4 by any number of hours you like.
  • DarrenRhodes
    DarrenRhodes over 2 years
    here's what I did in the end, axis.POSIXct(1, results$sample, at = seq(r[1], r[2], by = "12 hour"), format = "%a\n%H hrs", cex.axis = 0.75, las = 2), noting that by = "12 hour"