0

I have about a million data points of two columns: time and quantity. times are in 24 hours format (11:23:08 AM), and many of them are repeated many times (about 10-100 duplicates for each seconds). I am coding in R and I want a third column to sum (add) all the quantities for the last 5 seconds of time. the values for each specific seconds would become duplicates obviously.

It seems to be very easy but traditionally I just know writing two "for loops" in other programs to search for the condition of time in the last 5 seconds. However this would be very time-consuming in R . I need a second technique.

  • Can you provide example data? I think the times must contain a date in order to be handled correctly. This can be a dummy date, of course – eastclintw00d Jul 09 '19 at 21:40
  • I am not really sure the expected output you want. That's one of the reasons it is highly recommended that you always include a reproducible example (reprex) in your question. Take a look at this to find out more. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Having said that, if you are using tidyverse you should be doing something like data %>% group_by(last_5_seconds) %>% mutate(your_sum = sum(quantity)). – elikesprogramming Jul 09 '19 at 21:48

0 Answers0