I have two tibbles:
tbl_period
stores start and end time of each period.tbl_activity
contains the time and intensity of each activity.
How can I add columns to tbl_period
which counts number of activities and mean intensity within each period. Periods repeat and cannot be grouped together. So we need to group_by(id, period)
.
library(tidyverse)
set.seed(1)
period <- rep(c("A", "B", "C"), 3)
time <- sort(sample(1:100, 18, replace = FALSE))
# period
tbl_period <- tibble(period = period,
start = time[seq_along(time) %% 2 > 0],
end = time[seq_along(time) %% 2 == 0]) %>%
mutate(id = row_number())
# activity
tbl_activity <- tibble(time = sample(1:100, 50, replace = TRUE),
intensity = runif(50))