-2

I have a list of bank transactions for different customers and each transaction has a transaction time. I need to group the transactions into 24 bins, where each bin represents a one-hour interval, e.g: 8-9 am, 9-10 am, 22-23pm, and so on for 24 hours. So first I want to assign a bin number to each hour interval for each transaction, e.g: 8-9 am = "1", 9-10am = "2", and so on till 24. And then I will look at the frequency of each bin for each customer. e.g. is how many times does a customer shop between 4-5pm and so on for all the bins.
I have attached a screenshot of my what the data looks like.

enter image description here

I used

as.POSIXct(Customers$TRANSACTION.TIME,format="%H:%M:%S")

but the output it gives me looks like this enter image description here

The code I'm looking for should be something like this:

Customers$timebins = ifelse(
  Customers$TRANSACTION.TIME >= 8 & Customers$TRANSACTION.TIME < 9,
  1,
  ifelse(
    Customers$TRANSACTION.TIME >= 9 &
      Customers$TRANSACTION.TIME < 10,
    2,
    ifelse(........
    )

and so on.

f.lechleitner
  • 3,554
  • 1
  • 17
  • 35
Anadil
  • 1
  • 1

1 Answers1

0

If you want the bins to be hourly, this can be done very easily with Customers$timebins <- lubridate::hour(Customers$TRANSACTION.TIME) which will give you 24 hour long bins.

bmrn
  • 470
  • 3
  • 12
  • It gives me an error: Error in as.POSIXlt.character(as.character(x), ...) : character string is not in a standard unambiguous format – Anadil Jan 18 '18 at 11:51
  • What did you try next? What does this error tell you? – bmrn Jan 18 '18 at 11:57