在r中添加缺少的行到时间序列数据

时间:2016-08-08 18:33:41

标签: r

我有一个GPS坐标数据集,但有些时候会丢失。我希望使用适当的时间戳添加缺少的行,并为lat和lon列添加NA

这是当前格式化数据的方式:

             timestamp      lon       lat    id       date     time
                <time>    <dbl>     <dbl> <int>     <date>    <chr>
1  2012-08-01 03:59:00 36.92288 0.3508941     1 2012-08-01 03:59:00
2  2012-08-01 03:59:01 36.92288 0.3508901     1 2012-08-01 03:59:01
3  2012-08-01 03:59:02 36.92288 0.3508868     1 2012-08-01 03:59:02
4  2012-08-01 03:59:03 36.92288 0.3508828     1 2012-08-01 03:59:03
5  2012-08-01 03:59:05 36.92288 0.3508845     1 2012-08-01 03:59:05
6  2012-08-01 03:59:06 36.92288 0.3508866     1 2012-08-01 03:59:06
7  2012-08-01 03:59:07 36.92288 0.3508885     1 2012-08-01 03:59:07
8  2012-08-01 03:59:08 36.92288 0.3508903     1 2012-08-01 03:59:08
9  2012-08-01 03:59:09 36.92288 0.3508915     1 2012-08-01 03:59:09

你可以看到时间戳2012-08-01 03:59:04不见了我想在最后有这样的东西

             timestamp      lon       lat    id       date     time
                <time>    <dbl>     <dbl> <int>     <date>    <chr>
1  2012-08-01 03:59:00 36.92288 0.3508941     1 2012-08-01 03:59:00
2  2012-08-01 03:59:01 36.92288 0.3508901     1 2012-08-01 03:59:01
3  2012-08-01 03:59:02 36.92288 0.3508868     1 2012-08-01 03:59:02
4  2012-08-01 03:59:03 36.92288 0.3508828     1 2012-08-01 03:59:03
5  2012-08-01 03:59:04     NA      NA         1 2012-08-01 03:59:04
6  2012-08-01 03:59:05 36.92288 0.3508845     1 2012-08-01 03:59:05
7  2012-08-01 03:59:06 36.92288 0.3508866     1 2012-08-01 03:59:06
8  2012-08-01 03:59:07 36.92288 0.3508885     1 2012-08-01 03:59:07
9  2012-08-01 03:59:08 36.92288 0.3508903     1 2012-08-01 03:59:08
10 2012-08-01 03:59:09 36.92288 0.3508915     1 2012-08-01 03:59:09

对此有任何帮助将非常感谢!

1 个答案:

答案 0 :(得分:1)

使用Firefox 3.6 with Firebug 1.7.3 Firefox 4.0 with Firebug 1.7.3 Firefox 5.0 with Firebug 1.8.2 (and also Firebug 1.7.3) Firefox 6.0 with Firebug 1.8.2 (and also Firebug 1.9) Firefox 7.0 with Firebug 1.8.2 (and also Firebug 1.9) Firefox 8.0 with Firebug 1.8.3 (and also Firebug 1.9) Firefox 9.0 with Firebug 1.8.4 (and also Firebug 1.9) Firefox 10.0 with Firebug 1.9 Firefox 11.0 with Firebug 1.9 Firefox 12.0 with Firebug 1.9 Firefox 13.0 with Firebug 1.10 (and also Firebug 1.9) Firefox 14.0 with Firebug 1.10 Firefox 15.0 with Firebug 1.10 Firefox 16.0 with Firebug 1.10 Firefox 17.0 with Firebug 1.11 (and also Firebug 1.10) Firefox 18.0 with Firebug 1.11 Firefox 19.0 with Firebug 1.11 Firefox 20.0 with Firebug 1.11 Firefox 21.0 with Firebug 1.11 Firefox 22.0 with Firebug 1.11 Firefox 23.0 with Firebug 1.12 (and also Firebug 1.11) Firefox 24.0 with Firebug 1.12 Firefox 25.0 with Firebug 1.12 Firefox 26.0 with Firebug 1.12 Firefox 27.0 with Firebug 1.12 Firefox 28.0 with Firebug 1.12 Firefox 29.0 with Firebug 1.12 Firefox 30.0 with Firebug 2.0 (and also Firebug 1.12) Firefox 31.0 with Firebug 2.0 (and also Firebug 1.12) Firefox 32.0 with Firebug 2.0 (and also Firebug 1.12) Firefox 33 - 46 with Firebug 2.0 包中的rbind.fill

plyr

期望的输出:

ts_data <- read.table(text="timestamp,lon,lat,id,date,time
2012-08-01 03:59:00,36.92288,0.3508941,1,2012-08-01,03:59:00
2012-08-01 03:59:01,36.92288,0.3508901,1,2012-08-01,03:59:01
2012-08-01 03:59:02,36.92288,0.3508868,1,2012-08-01,03:59:02
2012-08-01 03:59:03,36.92288,0.3508828,1,2012-08-01,03:59:03
2012-08-01 03:59:05,36.92288,0.3508845,1,2012-08-01,03:59:05
2012-08-01 03:59:06,36.92288,0.3508866,1,2012-08-01,03:59:06
2012-08-01 03:59:07,36.92288,0.3508885,1,2012-08-01,03:59:07
2012-08-01 03:59:08,36.92288,0.3508903,1,2012-08-01,03:59:08
2012-08-01 03:59:09,36.92288,0.3508915,1,2012-08-01,03:59:09", 
                      header=T, stringsAsFactors=F, sep=",")

##Convert timestamp to datetime
ts_data$timestamp <- ts_data$timestamp <- as.POSIXct(strftime(ts_data$timestamp))

##Get full sequence
full_sequence <- seq(from=min(ts_data$timestamp), 
                     to=max(ts_data$timestamp), by="s")

##Grab the missing sequence
missing_sequence <- full_sequence[!(full_sequence %in% ts_data$timestamp)]

##Make a data.frame out of the missing sequence
missing_df <- data.frame(timestamp = missing_sequence, 
                         id = rep(1,length(missing_sequence)), 
                         date = strftime(missing_sequence, format = "%Y-%m-%d"), 
                         time=strftime(missing_sequence, format = "%H:%M:%S"))
##Combine the two
new_ts_data <- plyr::rbind.fill(ts_data, missing_df)
##Order by timestamp
new_ts_data <- new_ts_data[order(new_ts_data$timestamp),]

编辑以使用适当的数据集

已过滤的数据集已保存here,完整正常的脚本为here