# How do I round a column of time data to the nearest 15 minutes in r?

I have two data frames with time data every 15 minutes but one starts precisely on time (0:00, 0:15, 0:30, 0:45, etc.) and one starts slightly off (0:03, 0:18, 0:33, 0:48, etc). I would like to round the slightly off one to the nearest 15 minutes interval so that I may later merge the data frames so that the data corresponding to those times are in the same rows. The data is in 24 hour time and, as an example, in the format of:

``````Time
0:00
0:15
0:30
0:45
1:00
``````

I have tried the code below but r returns the error:

``````library(lubridate)
p_data\$Time <- round_date(p_data\$Time, "15 mins")

Error in UseMethod("reclass_date", orig) :
no applicable method for 'reclass_date' applied to an object of class "character"
In addition: Warning message: All formats failed to parse. No formats found.
``````

I then tried converting the time column from character to numeric but recieved the error:

``````p_data\$Time <- as.numeric(p_data\$Time)
Warning message:
NAs introduced by coercion
``````

I am very new to r (just started learning this week) so I apologize if this is due to a lack of common knowledge.

## Solution

Here an approach with lubridate. We first generate an arbitrary data set and then round it to 15minutes:

``````library(lubridate)
x <- seq(as.POSIXct("2021-05-19 10:00"), as.POSIXct("2021-05-19 11:00"), 240)
x

round_date(x, unit="15 mins")
``````

Edit: here the same idea with minutes only. We use a fake date, append the time, round it to 15min and extract minutes only:

``````library(lubridate)
x <- c("0:03", "0:18", "0:33", "0:48")
format(round_date(as.POSIXct(paste("1900-01-01 ", x)), unit="15 mins"), "%M")
``````