Home > front end >  R: Calculating the Number of Phone Calls Every Hour
R: Calculating the Number of Phone Calls Every Hour

Time:10-10

I have a dataset that looks like the following:

Call No.  Arrival Time  Call Length (in hrs) ...
================================================
1         0.01          0.061 
2         0.08          0.05
3         0.10                                   (Busy/Unanswered)
4         0.15          0.42
...
10        1.03          0.36
11        1.09          0.72
...

I want to count the number of phone calls each hour (e.g. number of successful phone calls from arrival times [0, 1), [1, 2), [2, 3), etc.

There are some empty values in the call length column, indicating that the phone line was busy, so the call went unanswered. I basically want to count the nonempty occurrences of the call length and group them by hour by summing them. How can I do this using dataframe operations in R?

CodePudding user response:

Perhaps this helps

library(dplyr)
df1 %>%
    group_by(Hour = round(`Arrival Time`)) %>%
    dplyr::summarise(Total_phone_calls = 
       sum(complete.cases(`Call Length (in hrs)`)))

Or remove the NA elements in the Call Length (in hrs)) column and use n() or count

library(tidyr)
df1 %>%
   drop_na(`Call Length (in hrs)`) %>%
   count(Hour = round(`Arrival Time`))
  • Related