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!