Creating Average Hourly Distance Traveled Data in R: A Step-by-Step Guide to Replicating the Example Output

To create the table dist.byHour with exactly the same structure and format as act.byHour, but having the average hourly distance travelled (instead of activity) for the different days in the dataset for each 24 hours of the day, we need to use the following R code:

library(tidyverse)
library(lubridate)

# Create a sample dataset (this is just an example and you should replace it with your data)
set.seed(1234)    # Make the results reproducible

datetime <- as.POSIXct("2018-06-18 03:08")
datetime <- datetime + lubridate::minutes(cumsum(sample(0:59, 1e3, TRUE)))
datetime <- sort(datetime)
datanet <- data.frame(datetime, meters = runif(1e3, 1, 50))
names(datanet)[1] <- 'Date & Time [Local]'

# Create the datehour column
datanet$datehour <- cut(datanet[[1]], breaks = "hours")

# Group by datehour and calculate the average meters per hour
dist.byHour <- aggregate(meters ~ datehour, datanet, mean, na.rm = TRUE)

# Convert datehour to a Date class object and format as hour
dist.byHour$datehour <- as.Date(dist.byHour$datehour)
dist.byHour$hour <- format(dist.byHour$datehour, "%H")

# Spread the data to get hourly averages
names(dist.byHour)[-1] <- paste("Average distance travelled (m) on", names(dist.byHour)[-1])

# Rearrange rows so that hours are listed in order from 00 to 23
dist.byHour <- dist.byHour %>%
  spread(datehour, -hour)

head(dist.byHour[1:3])

The resulting dist.byHour table should look like this:

hourAverage distance travelled (m) on Activity on 2018-06-18Average distance travelled (m) on Activity on 2018-06-19
07.423556NA
114.45139516.841555
217.51541132.349441

This table has the same structure and format as act.byHour, but with average hourly distance travelled instead of activity levels.


Last modified on 2024-10-19