-1

I have following date time column in R dataframe

date
2016-03-21 16:18:00 IST
2016-04-01 03:12:00 IST
2016-04-01 14:58:00 IST
2016-04-02 03:24:00 IST
2016-04-02 19:00:00 IST
2016-04-02 20:40:00 IST

Date column is in POSIXct format

I want to bucket time in 12am-6am,6am-12pm,12pm-6pm and 6pm-12am

My desired data frame would be

date                        time_bucket
2016-03-21 16:18:00 IST     12pm-6pm
2016-04-01 03:12:00 IST     12am-6am
2016-04-01 14:58:00 IST     12pm-6pm 
2016-04-02 03:24:00 IST     12pm-6pm
2016-04-02 19:00:00 IST     6pm-12am
2016-04-02 20:40:00 IST     6pm-12am 

How can I do it in r ?

Neil
  • 7,937
  • 22
  • 87
  • 145
  • 2
    Possible duplicate of [Group numeric values by the intervals](https://stackoverflow.com/questions/13559076/group-numeric-values-by-the-intervals) – Ronak Shah Jun 08 '17 at 08:08
  • 1
    [This](https://stackoverflow.com/questions/13649019/with-r-split-time-series-data-into-time-intervals-say-an-hour-and-then-plot-t) can be helpful too. – Ronak Shah Jun 08 '17 at 08:17
  • 1
    Did you try using `cut` ? SO is not a code-writing service where you say "I have this" and "I want this" without showing any efforts. You should also explain what have you tried to solve the problem and where did you get stuck. Also, being rude is not going to help you solve the problem. – Ronak Shah Jun 08 '17 at 08:52

2 Answers2

0

a little verbose but does the work (EDIT need lubridate package)

library(lubridate)
        tmp <- seq(as.POSIXct('2011-08-01 13:00'), as.POSIXct('2011-08-05 03:00'),
                       len=42)
            df <- data.frame(tm=tmp, x=seq(42))

            df$h<-substring(df$tm,12)
            df$h<-parse_date_time(df$h,"HMS")

            a<-df$h>=parse_date_time("00:00:00","HMS") & df$h<parse_date_time("06:00:00","HMS")
            b<-df$h>=parse_date_time("06:00:00","HMS") & df$h<parse_date_time("12:00:00","HMS")
            c<-df$h>=parse_date_time("12:00:00","HMS") & df$h<parse_date_time("18:00:00","HMS")
            d<-df$h>=parse_date_time("18:00:00","HMS") & df$h<parse_date_time("24:00:00","HMS")

            df<-cbind(df,a,b,c,d)

            df$group<-as.factor(1*a+2*b+3*c+4*d)
    levels(df$group)<-c("12am-6am", "6am-12pm", "12pm-6pm", "6pm-12am")
            df$h<-NULL
            df$a<-NULL
            df$b<-NULL
            df$c<-NULL
            df$d<-NULL
            df
Federico Manigrasso
  • 1,130
  • 1
  • 7
  • 11
-1

Since there are only four time slots I used a series of nested ifelse statemenst and the hour function from lubridate package

> data1=read.csv("data1.csv")
> library(lubridate)
> data1$hour=hour(data1$date)
> data1$timeslot=ifelse(data1$hour<6,"12am-6am",data1$hour)
> data1$timeslot=ifelse(6<=data1$hour&data1$hour<12,"6am-12pm",data1$timeslot)
> data1$timeslot=ifelse(12<=data1$hour&data1$hour<18,"12pm-6pm",data1$timeslot)
> data1$timeslot=ifelse(18<=data1$hour&data1$hour<=24,"6pm-12pm",data1$timeslot)
> data1
                     date hour timeslot
1 2016-03-21 16:18:00 IST   16 12pm-6pm
2 2016-04-01 03:12:00 IST    3 12am-6am
3 2016-04-01 14:58:00 IST   14 12pm-6pm
4 2016-04-02 03:24:00 IST    3 12am-6am
5 2016-04-02 19:00:00 IST   19 6pm-12pm
6 2016-04-02 20:40:00 IST   20 6pm-12pm
Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60