0

I am working with radio communications data that has the following data structure (P1-P5 are recipient columns):

Index   Onset   Terminus    Duration    Sender   Net   P1    P2       P3    P4   P5                                                         
1          1       4           3          P1     N1     0     1        1     1    0                                                         
2          6      10           4          P2     N2     0     0        1     1    0                                                     
3         11      16           5          P3     N2     0     1        1     1    0     

I am trying to convert the data into a structure like this:

Time    Onset   Terminus    Duration    Sender   Net   P1    P2       P3    P4   P5                                                         
  1        1       4           3          P1     N1     0     1        1     1    0     
  2        1       4           3          P1     N1     0     1        1     1    0  
  3        1       4           3          P1     N1     0     1        1     1    0  
  4        1       4           3          P1     N1     0     1        1     1    0  
  5       n/a     n/a         n/a        n/a    n/a    n/a   n/a      n/a   n/a  n/a
  6        6       10          4          P2     N2     0     0        1     1    0

In other words, to format the data for time-series analysis while preserving everything else. It looks like I need to add a row for every missing unit of time, and also expand the 'Onset' and 'Terminus' columns into individual units of time. I would appreciate any help for this! Thank you, g

1 Answers1

0

Here's an approach, but I bet there's a shorter way:

library(tidyverse)
df %>%
  uncount(Terminus - Onset + 1) %>%
  group_by(Index) %>% mutate(instance = row_number()) %>% ungroup() %>%
  mutate(Time = Onset + instance - 1) %>%
  padr::pad_int("Time")
Jon Spring
  • 55,165
  • 4
  • 35
  • 53