如何将具有开始和结束时间列的行转换为 R 中的时间序列格式?

How can I convert rows with start and end time columns into time-series format in R?

我正在处理具有以下数据结构的无线电通信数据(P1-P5 是收件人列):

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     

我正在尝试将数据转换成这样的结构:

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

换句话说,为时间序列分析格式化数据,同时保留其他所有内容。看起来我需要为每个缺失的时间单位添加一行,并将 'Onset' 和 'Terminus' 列扩展为单独的时间单位。 我将不胜感激任何帮助! 谢谢, g

这是一种方法,但我敢打赌还有一种更短的方法:

library(tidyverse)
df %>%
  uncount(Terminus - Onset + 1) %>%
  group_by(Index) %>% mutate(instance = row_number()) %>% ungroup() %>%
  mutate(Time = Onset + instance - 1) %>%
  padr::pad_int("Time")