SHIRLEEN
SHIRLEEN

Reputation: 89

how to get average time in a column in hr:min format in r?

I have the datalike

mode       departure_time    activity_purpose
car            7:08            work
car            8:45            work
bike           6:34            school
walk           5:45            work
walk           6:20            school

how to calculate the average departure time for each activity purpose? And how to draw graph shows the departure time distribute? the y should be count and x should be departure time with format 5:00, 6:00, 7:00 etc.

Thanks for all the help!

Upvotes: 2

Views: 210

Answers (2)

TarJae
TarJae

Reputation: 79246

Is this what you are looking for:

  1. use period_to_seconds function from lubridate to get period element
  2. group_by whatever you need: here you get the average departure_time of activity_purpose (you can adapt as you need)
  3. count
  4. to plot mutate to dttm element
  5. plot bar chart
library(tidyverse)
library(lubridate)

tib1 <- tib %>%
  group_by(activity_purpose) %>% 
  mutate(average = seconds_to_period(mean(period_to_seconds(hm(departure_time))))) %>% 
  count(average)

tib2 <- tib1 %>% 
  mutate(average_date_time = floor_date(Sys.time(), "1 day") + average)

ggplot(data=tib2, aes(x=average_date_time, y=n)) + 
  geom_bar(stat = "identity")

Output:

# Groups:   activity_purpose [2]
  activity_purpose average        n
  <chr>            <Period>   <int>
1 school           6H 27M 0S      2
2 work             7H 12M 40S     3

enter image description here

Upvotes: 1

Baraliuh
Baraliuh

Reputation: 593

Here is one way of doing:

library(dplyr)
#Read in data
tib <- tibble::tribble(
  ~mode,       ~departure_time,    ~activity_purpose,
  'car',            '7:08',            'work',
  'car',            '8:45',            'work',
  'bike',           '6:34',            'school',
  'walk',           '5:45',            'work',
  'walk',           '6:20',            'school'
)
#Define function for mean time calculation
calc_mean_depart <- function(times){
  #Extract hrs from time
  hrs <- stringr::word(times, 1, sep = ':') %>% 
    #Convert to integer
    as.integer() %>% 
    #Convert to minutes
    magrittr::multiply_by(60)
  #Extract minutes
  min <- stringr::word(times, 2, sep = ':') %>% 
    #Convert to integer
    as.integer()
  #Get mean time in minutes
  mean_time <- mean(hrs + min)
  #Get the average number of hrs
  mean_hrs <- floor(mean_time/60)
  #Generate the output
  paste0(
    mean_hrs, ':', round(mean_time %% 60) ## Time mod 60 == minutes
  )
}
#Calculate group-wise mean time
tib %>% 
  #Group on activity
  dplyr::group_by(activity_purpose) %>% 
  #Get the mean
  dplyr::summarise(
    mean = calc_mean_depart(departure_time)
  )
#Output:
# A tibble: 2 x 2
  activity_purpose mean 
  <chr>            <chr>
1 school           6:27 
2 work             7:13
#Plot departure hr
tib %>% 
  dplyr::mutate(
    hrs = stringr::word(departure_time, 1, sep = ':'),
    hrs = as.integer(hrs)
  ) %>% 
  ggplot2::ggplot(ggplot2::aes(hrs))+
  ggplot2::geom_histogram(binwidth = 1)

Upvotes: 1

Related Questions