How can I to categorize a timestamp column in an interval of 2 hours in R?

Eric Gomes

I need to categorize a column where the date is a DateTime field (timestamp) like the image below:

enter image description here

To do this, I need to create a new column in my DataFrame to receive the new data, where the result is a column categorized with interval 2 hours like the image above.

Result:

enter image description here

How can I do this?

Ronak Shah

You can use floor_date/ceiling_date from lubridate

library(dplyr)
library(lubridate)

df <- data.frame(time = Sys.time() + sample(100000, 10))

df %>%
  mutate(interval = hour(floor_date(time, '2 hours')), 
         interval1 = paste(interval, interval + 2, sep = '-'), 
         interval2 = sprintf('[%d, %d)', interval, interval + 2, sep = '-'))

#                  time interval interval1 interval2
#1  2021-06-07 13:14:15       12     12-14  [12, 14)
#2  2021-06-07 14:57:42       14     14-16  [14, 16)
#3  2021-06-06 23:42:58       22     22-24  [22, 24)
#4  2021-06-07 07:11:57        6       6-8    [6, 8)
#5  2021-06-08 01:20:38        0       0-2    [0, 2)
#6  2021-06-07 17:51:25       16     16-18  [16, 18)
#7  2021-06-07 16:15:47       16     16-18  [16, 18)
#8  2021-06-07 11:29:56       10     10-12  [10, 12)
#9  2021-06-07 16:59:13       16     16-18  [16, 18)
#10 2021-06-07 11:47:07       10     10-12  [10, 12)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to categorize timestamp hours into day and night?

how can I categorize based on several column

MySQL - select interval of every 2 hours from timestamp column

How to add interval hours to a timestamp in postgres, excluding weekend hours

How can I categorize numerical columns

How can I categorize emojis in emojione library?

In R, how do I split timestamp interval data into regular slots?

How can I add two hours to a timestamp value then reconvert it to a string with only the hours and minutes?

How can I add a timestamp column in hive

SPARK - How can I parse a column to timestamp?

R function categorize by column?

I have dataframe column with time in integer format, I want to convert it into hours and mins and categorize them into bins

How can I create a TIMESTAMP column in HIVE with a string based timestamp?

How can I convert string timestamp column to timestamp in grafana

SQL Server: How to group by a datetime column based on a time interval (Such as within 2 hours)

R: How can I check the format of a timestamp?

How can I scale the time (hours) of my x-axis in ggplot2 in r?

How to i add 24 hours to 12 hours timestamp with AM and PM?

How do I get timestamp interval in swift?

How to add amount of hours determined from a column to a timestamp column?

How can i add days to a Hive timestamp without loosing hours, minutes and seconds

How Can I Convert a String to Hours, Minutes, and Seconds in R?

How can I extract a specific range of hours from a dataframe in R

How can I categorize the dimension values in 3 different categories?

How can I use apply in pandas to categorize my codes?

How can I categorize files in a directory based on their content?

How can I categorize numbers that inside a text file?

DataBricks 10.2 pyspark 3.2.0; How Do I Add a New Timestamp Column Based on Another Date and Integer (Hours) Column?

How can I restrict users of computers to 2 hours only?