# How to generate time intervals or date sequence in R

In this post, you will get summary and code examples for creating time intervals, date or date-time sequence different ways in R.

For example, you have to make summary statistics for 15 minute time intervals in R. There might be situations wherein a 15 minute interval is no data. It is necessary to, first of all, generate theoretical hour and minute sequence and then join with actual data. Notice that for actual data sometimes it is necessary to detect last full-time interval.

For that task, you will need chron and lubridate libraries and some of the base functions like merge and seq.

## Hour and minute (time) intervals

In my case, there is 15 minute sequence that I’m creating with seq(0, 45, by = 15) and merging with hour vector. By merging two vectors, you have a data frame with two columns with names x, y all possible combinations.

```# hour & 15 minute intervals

hm <- merge(0:23, seq(0, 45, by = 15))```

With paste function, you can combine them in something that looks like time and convert it to times class with chron function from chron package.

```# hour & 15 minute intervals

hm <- merge(0:23, seq(0, 45, by = 15))

chron(time = paste(hm\$x, ':', hm\$y, ':', 0))
```

You can also put those time intervals in the data frame and order.

```# hour & 15 minute intervals

hm <- merge(0:23, seq(0, 45, by = 15))

chron(time = paste(hm\$x, ':', hm\$y, ':', 0))

interval_15min <- data.frame('INTERVAL' = chron(time = paste(hm\$x, ':', hm\$y, ':', 0)))
interval_15min <- data.frame('INTERVAL' = interval_15min[order(interval_15min\$INTERVAL), ])```

## Last full time interval

If there is need to work with today’s data and you have data from a certain minute interval that is not ended, then sometimes you need to calculate last time full interval. For example, to subset data that will not change.

There are two functions from lubridate library floor_date and ceiling_date for rounding to the nearest time unit. In my case, I round down to nearest 15 minute interval, but last fully closed time interval is 15 minutes before, and that is the reason why I’m subtracting 900 (seconds).

```# last full 15 minute time interval with date

strftime(floor_date(as.POSIXct(Sys.time()), '15 minutes') - 900, format = '%Y-%m-%d %H:%M:%S')```

## Time from date time

You can also return only time portion and covert to times class with chron if necessary.

```# last full 15 minute time interval with date

strftime(floor_date(as.POSIXct(Sys.time()), '15 minutes') - 900, format = '%Y-%m-%d %H:%M:%S')

# last full 15 minute time interval without date

chron(time = strftime(floor_date(as.POSIXct(Sys.time()), '15 minutes') - 900, format = '%H:%M:%S'))```

## Date sequence

Sometimes you have to combine date sequence and earlier created time intervals. You can create a date sequence in R easily with base function.

```# date sequence

seq.Date(from = as.Date('2019-07-01'), to = as.Date('2019-07-10'), by = 'days') # base```

You can also make a date sequence with the help of lubridate library, but it looks a little bit slower.

```# date sequence

seq.Date(from = as.Date('2019-07-01'), to = as.Date('2019-07-10'), by = 'days') # base

seq(from = ymd('2019-07-01'), to = ymd('2019-07-10'), by='days') # lubridate```

If you want to build a dynamic date sequence in R (based on today’s date), for example, for the last 7 days, it goes like this.

```# date sequence

seq.Date(from = as.Date('2019-07-01'), to = as.Date('2019-07-10'), by = 'days') # base

seq(from = ymd('2019-07-01'), to = ymd('2019-07-10'), by='days') # lubridate

# last 7 days sequence

Sys.Date() - 1:7```

## Date and time interval sequence

There is nothing much to say – combine previous techniques. After merging date sequence and time intervals, change column names. Combine date and time together and make it right order. I also made reset for row numbers after ordering.

```# date, hour & minute intervals

last7days <- Sys.Date() - 1:7
hm <- merge(0:23, seq(0, 45, by = 15))

datetime <- merge(last7days, chron(time = paste(hm\$x, ':', hm\$y, ':', 0)))
colnames(datetime) <- c('date', 'time')

# create datetime
datetime\$dt <- as.POSIXct(paste(datetime\$date, datetime\$time))
# create right order
datetime <- datetime[order(datetime\$dt), ]
row.names(datetime) <- NULL```

## Here is all the code

```library(chron)
library(lubridate)

# hour & 15 minute intervals

hm <- merge(0:23, seq(0, 45, by = 15))

chron(time = paste(hm\$x, ':', hm\$y, ':', 0))

interval_15min <- data.frame('INTERVAL' = chron(time = paste(hm\$x, ':', hm\$y, ':', 0)))
interval_15min <- data.frame('INTERVAL' = interval_15min[order(interval_15min\$INTERVAL), ])

# last full 15 minute time interval with date

strftime(floor_date(as.POSIXct(Sys.time()), '15 minutes') - 900, format = '%Y-%m-%d %H:%M:%S')

# last full 15 minute time interval without date

chron(time = strftime(floor_date(as.POSIXct(Sys.time()), '15 minutes') - 900, format = '%H:%M:%S'))

# date sequence

seq.Date(from = as.Date('2019-07-01'), to = as.Date('2019-07-10'), by = 'days') # base

seq(from = ymd('2019-07-01'), to = ymd('2019-07-10'), by='days') # lubridate

# last 7 days sequence

Sys.Date() - 1:7

# date, hour & minute intervals

last7days <- Sys.Date() - 1:7
hm <- merge(0:23, seq(0, 45, by = 15))

datetime <- merge(last7days, chron(time = paste(hm\$x, ':', hm\$y, ':', 0)))
colnames(datetime) <- c('date', 'time')

# create datetime
datetime\$dt <- as.POSIXct(paste(datetime\$date, datetime\$time))
# create right order
datetime <- datetime[order(datetime\$dt), ]
row.names(datetime) <- NULL
```

1. IL