Creating a specific sequence of date/times in R
I would strongly recommend you to use the POSIXct
datatype. This way you can use seq
without any problems and use those data however you want.
start <- as.POSIXct("2012-01-15")
interval <- 60
end <- start + as.difftime(1, units="days")
seq(from=start, by=interval*60, to=end)
Now you can do whatever you want with your vector of timestamps.
How to generate a sequence of dates and times with a specific start date/time in R
No need for lubridate, just,R code:
x <- data.frame(date = c(as.POSIXct("2018-01-01 00:00:00"), rep(NA,10)))
startDate <- x[["date"]][1]
x[["date2"]] <- startDate + (seq_len(nrow(x)) - 1)
x
# date date2
# 1 2018-01-01 2018-01-01 00:00:00
# 2 <NA> 2018-01-01 00:00:01
# 3 <NA> 2018-01-01 00:00:02
# 4 <NA> 2018-01-01 00:00:03
# 5 <NA> 2018-01-01 00:00:04
# 6 <NA> 2018-01-01 00:00:05
# 7 <NA> 2018-01-01 00:00:06
# 8 <NA> 2018-01-01 00:00:07
# 9 <NA> 2018-01-01 00:00:08
# 10 <NA> 2018-01-01 00:00:09
# 11 <NA> 2018-01-01 00:00:10
Create a sequence of times in R?
We can use seq
:
format(seq(as.POSIXct('00:00', format = "%H:%M", tz = "UTC"),
as.POSIXct(Sys.Date() + 1), by = '10 mins'), "%I:%M%p")
#[1] "12:00AM" "12:10AM" "12:20AM" "12:30AM" "12:40AM" "12:50AM" "01:00AM ...
#[141] "11:20PM" "11:30PM" "11:40PM" "11:50PM" "12:00AM"
Make sure you have the correct locale or set it via :
Sys.setlocale("LC_TIME", "en_US.UTF-8")!
Create a regular sequence of date-times (POSIXct) using seq()
There is a seq()
method for objects of class "POSIXt"
which is the super class of the "POSIXlt"
and "POSIXct"
classes. As such you don't need to do any conversion.
> now <- Sys.time()
> tseq <- seq(from = now, length.out = 100, by = "mins")
> length(tseq)
[1] 100
> head(tseq)
[1] "2012-01-19 10:52:38 GMT" "2012-01-19 10:53:38 GMT"
[3] "2012-01-19 10:54:38 GMT" "2012-01-19 10:55:38 GMT"
[5] "2012-01-19 10:56:38 GMT" "2012-01-19 10:57:38 GMT"
Create a sequence of dates and store in a data frame with a column name
There are lot of ways to do that, since you used data.frame :
library(lubridate)
library(magrittr)
seq.Date(from = as.Date(today()- days(7)),
to = as.Date(today()),
by = "day") %>%
data.frame(DATE = .)
# DATE
#1 2021-04-23
#2 2021-04-24
#3 2021-04-25
#4 2021-04-26
#5 2021-04-27
#6 2021-04-28
#7 2021-04-29
#8 2021-04-30
Creating a unique sequence of dates
As I noted in my comment, seq
has method for dates, seq.Date
:
seq(as.Date('2011-01-01'),as.Date('2011-01-31'),by = 1)
[1] "2011-01-01" "2011-01-02" "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06" "2011-01-07" "2011-01-08"
[9] "2011-01-09" "2011-01-10" "2011-01-11" "2011-01-12" "2011-01-13" "2011-01-14" "2011-01-15" "2011-01-16"
[17] "2011-01-17" "2011-01-18" "2011-01-19" "2011-01-20" "2011-01-21" "2011-01-22" "2011-01-23" "2011-01-24"
[25] "2011-01-25" "2011-01-26" "2011-01-27" "2011-01-28" "2011-01-29" "2011-01-30" "2011-01-31"
Create sequence of dates and times in R without time zones
The problem is that since it is adjusting for BST, I get two values for certain dates in October.
That's because the 'fall back' (mnemonic for daylight savings times adjustment adding an hour in the fall) happens under human time and that is what you get by default unless you override it.
R> seq(as.POSIXlt("2012-10-28 00:00:00", tz="UTC"),
+ as.POSIXlt("2012-10-28 03:00:00", tz="UTC"), by="15 min")
[1] "2012-10-28 00:00:00 UTC" "2012-10-28 00:15:00 UTC"
[3] "2012-10-28 00:30:00 UTC" "2012-10-28 00:45:00 UTC"
[5] "2012-10-28 01:00:00 UTC" "2012-10-28 01:15:00 UTC"
[7] "2012-10-28 01:30:00 UTC" "2012-10-28 01:45:00 UTC"
[9] "2012-10-28 02:00:00 UTC" "2012-10-28 02:15:00 UTC"
[11] "2012-10-28 02:30:00 UTC" "2012-10-28 02:45:00 UTC"
[13] "2012-10-28 03:00:00 UTC"
R>
The example I show here covers the same subset as above but without the fall back as we now impose UTC as a time zone. And UTC has be construction no daylight savings adjustment.
How to use seq() to create column of date/times with increments of milliseconds (deciseconds)
This might work for you:
library(lubridate)
df <- data.frame(ID = 1:4,
CH_1 = rep(c(-10096, -10088, -10084, -10088), 4),
CH_2 = rep(c(-11940, -11964, -11940, -11956), 4),
date_time = c("2018-07-24 10:45:01.1", rep(NA, 15)))
df$date_time <- as.POSIXct(df$date_time, format= "%F %H:%M:%OS2", tz = "UTC")
df$date_time <- seq(from = df$date_time[1], to = df$date_time[1] + (nrow(df)-1)*0.1, by = 0.10)
format(df$date_time, "%OS3")
Create sequence of date on every last day of month
If you want last day of month, instead of start from 2018-01-31
, try
seq(as.Date("2018-02-01",format="%Y-%m-%d"),by="month",length.out=6) -1
[1] "2018-01-31" "2018-02-28" "2018-03-31" "2018-04-30" "2018-05-31" "2018-06-30"
Related Topics
How to Rename Element's List Indexed by a Loop in R
Warning: Replacing Previous Import 'Head' When Loading 'Utils' in R
Drawing Non-Intersecting Circles
Filled and Hollow Shapes Where the Fill Color = the Line Color
How to Sort Data by Column in Descending Order in R
Getting the Error "Level Sets of Factors Are Different" When Running a for Loop
Setting Individual Y Axis Limits with Facet Wrap Not with Scales Free_Y
Function Composition in R (And High Level Functions)
Return a List in Dplyr Mutate()
Merging Data.Tables Based on Columns Names
"Could Not Find Function" in Roxygen Examples During Cmd Check
Error When Plotting Sf Object --- Error: Could Not Find Function "Geom_Sf"
How to Show Every Second R Ggplot2 X-Axis Label Value
Substitute a for B and B for a in a String
R Obtaining Rownames Date Using Quantmod