如何从不规则面板时间序列数据生成规则面板

时间:2019-02-16 19:01:18

标签: r time-series panel

我想从不规则的面板数据结构变为规则的面板数据结构,但是我在努力做到这一点。任何建议欢迎!

当前不规则面板数据结构:

trackingid <- as.character(c(1470149111625446735))
timestamp <- as.character(c("2018-06-17", "2018-06-18", "2018-06-19", "2018-06-21", "2018-06-22", "2018-06-23"))
pageimp <- as.numeric(c(8, 1, 3, 4, 2, 3))
dt <- data.frame(trackingid,timestamp, pageimp)

期望的常规面板数据结构:

trackingid <- as.character(c(1470149111625446735))
timestamp <- as.character(c("2018-06-17", "2018-06-18", "2018-06-19", "2018-06-20", "2018-06-21", "2018-06-22", "2018-06-23"))
pageimp <- as.numeric(c(8, 1, 3, 0, 4, 2, 3))
dt <- data.frame(trackingid,timestamp, pageimp)

请注意,在我的完整数据中,我将获得更多具有不同不规则时间戳记的trackingid。到目前为止,所有先前的解决方案都只讨论了从不规则的时间序列过渡到规则的时间序列,而不考虑数据的面板性质。

1 个答案:

答案 0 :(得分:1)

可以:

library(tidyverse)

dt %>%
  mutate(timestamp = as.Date(timestamp)) %>%
  group_by(trackingid) %>%
  complete(timestamp = seq(min(timestamp), max(timestamp), by = "day"), fill = list(pageimp = 0))

输出:

# A tibble: 7 x 3
# Groups:   trackingid [1]
  trackingid          timestamp  pageimp
  <fct>               <date>       <dbl>
1 1470149111625446656 2018-06-17       8
2 1470149111625446656 2018-06-18       1
3 1470149111625446656 2018-06-19       3
4 1470149111625446656 2018-06-20       0
5 1470149111625446656 2018-06-21       4
6 1470149111625446656 2018-06-22       2
7 1470149111625446656 2018-06-23       3

基本上,您将trackingid分组,将数据从最小值timestamp扩展到最大值fill,并使用.container{ width: 100%; height:100% } .links{ float: right; } .container .infoBar { -webkit-transform: translateY(-50px); -webkit-animation: seeSaw 2.5s 1.0s 1 ease forwards; -moz-transform: translateY(-50px); -moz-animation: seeSaw 2.5s 1.0s 1 ease forwards; } .infoBar{ position: absolute; z-index: 101; top: 0; left: 0; right: 0; background: #fde073; text-align: center; line-height: 2.5; overflow: hidden; -webkit-box-shadow: 0 0 5px black; -moz-box-shadow: 0 0 5px black; box-shadow: 0 0 5px black; } @keyframes seeSaw{ 0%, 100% { transform: translateY(-50px); } 10%, 90% { transform: translateY(0px); } } .complete { background-color: #e7f3da; color: #7db742; border: 1px solid #7db742; }参数填充任何缺少0的内容。