Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: Calculating the Number of Phone Calls Every Hour

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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`))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading