Home > Mobile >  How to find the number of requests per second/hour/day from csv file in R
How to find the number of requests per second/hour/day from csv file in R

Time:12-17

I have a csv file with the following data:

#type_of_r,date_time

request1,2022.12.09 12:03:06.836
reqeust2,2022.12.09 12:03:06.897
request3,2022.12.09 12:04:07.840
request1,2022.12.09 12:05:07.220
request1,2022.12.09 12:10:08.001
request2,2022.12.09 21:40:08.005

At the moment the file contains the information relative to a 1 day period. Now I need to obtain a graph showing how many requests per second, per hour and per day.

CodePudding user response:

library(dplyr)
library(lubridate)


df <-
  structure(list(request = c("request1", "request2", "request3", "request1", 
                        "request1", "request2"),
                 dttm = c("2022.12.09 12:03:06.836", "2022.12.09 12:03:06.897", 
                        "2022.12.09 12:04:07.840", "2022.12.09 12:05:07.220", "2022.12.09 12:10:08.001", 
                        "2022.12.09 21:40:08.005")),
            class = "data.frame", row.names = c(NA,-6L)) %>% 
  mutate(
    dttm = ymd_hms(dttm),
    day = day(dttm),
    hour = hour(dttm),
    minute = minute(dttm),
    second = second(dttm)
    )
  
#requests by second/minute/hour
df %>% 
  count(hour,minute,second)

#requests by hour
df %>% 
  count(hour)

#requests by day
df %>% 
  count(day)
  • Related