Finding Out the Number of Popcorn Kernels that Popped Each 70 Seconds?

I am working with the R programming language. Suppose I have the following data that contains information on the time at which each kernel of popcorn popped:

library(dplyr)
    
    popping_times = abs(rnorm(40,10,3))
    index = 1:40
    my_data = data.frame(index, popping_times)

  index popping_times
1     1     15.183441
2     2     16.448910
3     3     11.251157
4     4      5.904881
5     5      8.794389
6     6      6.787689

I can now find the cumulative popping times:

my_data$cumulative = cumsum(my_data$popping_times)

  index popping_times cumulative
1     1     15.183441   15.18344
2     2     16.448910   31.63235
3     3     11.251157   42.88351
4     4      5.904881   48.78839
5     5      8.794389   57.58278
6     6      6.787689   64.37047

I want to find out the number of popcorn that popped every 70 seconds - I know a very "lazy" way to do this:

max(my_data$cumulative)/70
[1] 5.881913

my_data$number_of_events_per_70 = as.factor(ifelse(my_data$cumulative<70,1, ifelse(my_data$cumulative > 70 & my_data$cumulative<140, 2, ifelse(my_data$cumulative>140 & my_data$cumulative<210, 3, ifelse(my_data$cumulative>210 & my_data$cumulative<280, 4, ifelse(my_data$cumulative>280 & my_data$cumulative<350, 5, 6))))))

> groups = my_data %>% group_by(number_of_events_per_70) %>% summarise(count = n())
> groups
# A tibble: 6 x 2
  number_of_events_per_70 count
  <fct>                   <int>
1 1                           6
2 2                           7
3 3                           8
4 4                           7
5 5                           7
6 6                           5

My Question: Is there an easier way to do this? For example, suppose I want to find out the number of popcorn kernels that popped every 13 seconds - I would have to restart and write a VERY long series of "ifelse" statements. Is there a more direct way to do this?

Thanks!

Use integer division, %/%, to label the times. For example, I can assign the integers from 0 to 30 to bins that span 13 numbers like this:

x <- 0:30 
> names(x) <- 0:30
> x %/% 13
 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 
 0  0  0  0  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1 
25 26 27 28 29 30 
 1  2  2  2  2  2 
2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.