Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中将具有开始和结束时间列的行转换为时间序列格式?_R_Time Series_Sna - Fatal编程技术网

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

如何在R中将具有开始和结束时间列的行转换为时间序列格式?,r,time-series,sna,R,Time Series,Sna,我正在使用具有以下数据结构的无线电通信数据(P1-P5为收件人列): 我正在尝试将数据转换为如下结构: Time Onset Terminus Duration Sender Net P1 P2 P3 P4 P5 1 1 4 3 P1 N1 0

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

我正在尝试将数据转换为如下结构:

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
换句话说,为时间序列分析格式化数据,同时保留所有其他内容。看起来我需要为每个缺少的时间单位添加一行,还需要将“开始”和“结束”列扩展到各个时间单位。 我将感谢任何帮助! 非常感谢。
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")
library(tidyverse)
df %>%
  uncount(Terminus - Onset + 1) %>%
  group_by(Index) %>% mutate(instance = row_number()) %>% ungroup() %>%
  mutate(Time = Onset + instance - 1) %>%
  padr::pad_int("Time")