map_hourly_interval_dfr: Apply a function to each group and hourly interval in a...

View source: R/map-interval.r

map_hourly_interval_dfrR Documentation

Apply a function to each group and hourly interval in a data.frame

Description

For checking data represented as a data frame, a column contatining timestamps, and other information associated with the checkin, this functions applies a specified function to the subsets of the data falling within the time interval. This can be used to get check in counts for each hour, for example. In addition, grouped data is supported (using dplyr::group_by) to further specify subsets of specific groups and time intervals on which the function should be applied. The map_hourly_interval_dfr() function appends the results by row and map_hourly_interval_dfc() function appends results by column.

Usage

map_hourly_interval_dfr(.x, .f, time, end = NULL, ...)

Arguments

.x

a data.frame to apply a function to.

.f

the function to apply to the group and hourly interval.

time

the column denoting the checkin times.

end

the end time. By default this is the the end of the hour corresponding to the largest time.

...

other arguments to pass to the function .f.

Examples

# Count the number of transitions between locations in the checkin data.

library(dplyr)

data(checkins)

 from_to <- function(it) {
   it$duration <- c(diff(it$timestamp), 0)
   units(it$duration) <- "secs"
   it$duration <- as.numeric(it$duration)
   from_duration <- sum(it$duration[it$location == it$location[1]])
   tibble(from=it$location[1],
          to = it$location[nrow(it)],
          timestamp = it$timestamp[1],
          from_duration = from_duration)
 }

 y <- checkins %>%
   head(100) %>%
   group_by(id) %>%
   map_hourly_interval_dfr(from_to, time = "timestamp") 


kaneplusplus/checkin documentation built on Aug. 1, 2022, 1:11 p.m.