Lee
Lee

Reputation: 119

Rounding up the time expression in R

I have a data Y . Y has a column time . time column looks like this:

enter image description here

For example, 20211201000010 means 2021-12-01 00:00:10 .

time <- strptime(Y$time, format = "%Y%m%d%H%M%S")

start_time <- min(time)

In this code, start_time is 2021-12-01 00:00:02.

But I want to round up the start_timeas 2021-12-01 00:00:10,since the start_time should be 10 seconds interval for my data.

How can I round up 2021-12-01 00:00:02 as 2021-12-01 00:00:10 ?

Upvotes: 0

Views: 41

Answers (2)

HMH1013
HMH1013

Reputation: 1424

You may need to calculate the remainder (divide by 10) before you format the data.

e.g.

20211201000002 %% 10 = 2;
20211201000010 %% 10 = 0

Then you find the first 0 in your list.

Upvotes: 0

Quixotic22
Quixotic22

Reputation: 2924

lubridate package is always our friends for datetime work.


library(lubridate)      
xx1 <- '20211201010002'

ymd_hms(xx1) %>%
  ceiling_date(unit = '10s')

[1] "2021-12-01 01:00:10 UTC"

Upvotes: 2

Related Questions