1

I have two tibbles:

  1. tbl_period stores start and end time of each period.
  2. 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))
SiH
  • 1,378
  • 4
  • 18
  • Your `tbl_period` definition throws an error---you can't use `row_number()` inside `tibble`. And you should probably specify a `set.seed()` to make your random numbers reproducible. – Gregor Thomas Dec 01 '21 at 20:18
  • That said, the answer is a combination of 2 FAQs. You need to do a non-equi join which you can do [using one of these methods](https://stackoverflow.com/q/37289405/903061) and followed by a [sum by group](https://stackoverflow.com/q/1660124/903061). – Gregor Thomas Dec 01 '21 at 20:21
  • I have fixed the question. I will check the methods. Thanks – SiH Dec 01 '21 at 20:44

0 Answers0