我有一个数据集,其中时间被表示为连续的时段(即从时间1到时间2),就像这样:
d <- data.frame(id = c("A","A","B","B","C","C"),
t1 = c(1,3,1,3,1,3),
t2 = c(2,4,2,4,2,4),
value = 1:6)
我希望把这个数据重构成面板数据集,即每个单位和时间段都对应一行,如下所示:
result <- data.frame(id = c("A","A","A","A","B","B","B","B","C","C","C","C"),
t= c(1:4,1:4,1:4),
value = c(1,1,2,2,3,3,4,4,5,5,6,6))
我试图使用 tidyr
和 gather
完成这个任务,但未能获得期望的结果。我尝试了以下代码,但显然是错误的:
gather(d, 't1', 't2', key=t)
在实际数据集中,拼写不规则。
gather()
、select()
和arrange()
函数。 - Roman