将两个as.POSIXct(digits.sec = 2)带时间戳的数据集合并为具有不规则频率的特定数据问题

时间:2017-04-05 16:52:03

标签: r csv merge data.table posixct

我正在尝试合并两个具有不规则时间戳频率的数据集。我在另一篇文章中跟随了一个例子来尝试使这个工作,但它仍然不会合并。我试图遵循的例子是adding data from an irregular time series to a timeseries with 5-min timesteps

数据集1

Words <- as.character(c("2016-08-30 15:04:51.97", "2016-08-30 15:04:53.70",
                           "2016-08-30 15:04:54.26", "2016-08-30 15:04:56.00",
                           "2016-08-30 15:04:56.55", "2016-08-30 15:04:58.29",
                           "2016-08-30 15:04:58.85", "2016-08-30 15:05:00.59",
                           "2016-08-30 15:05:01.15", "2016-08-30 15:05:02.89",
                           "2016-08-30 15:05:03.45", "2016-08-30 15:05:05.19",
                           "2016-08-30 15:05:05.75", "2016-08-30 15:05:07.49",
                           "2016-08-30 15:05:08.04"))
    op <- options(digits.secs = 2)
    op
    Date <- as.POSIXct(Words)
    TagID <- rep(2297.2, 15)
    Xaxis <- as.numeric(c(13.738267, 13.76611, 13.728986, 13.70624, 13.722799, 
                          13.696131, 13.707635, 13.683349, 13.688462, 13.690102,
                          13.67994, 13.680669, 13.684442, 13.676477, 13.678154))
    Yaxis <- as.numeric(c(14.670887, 14.630401, 14.684383, 14.68586, 14.69338,
                          14.686517, 14.694365, 14.677797, 14.681285, 14.687439, 
                          14.675471, 14.678207, 14.681899, 14.674103, 14.675745))
    Zaxis <- as.numeric(c(10.106183, 10.198599, 10.075378, 10.057535, 10.054841,
                          10.049604, 10.042946, 10.057003, 10.054044, 10.043906, 
                          10.058976, 10.054471, 10.050245, 10.059166, 10.057288))
    Data1 <- data.frame(Date, TagID, Xaxis, Yaxis, Zaxis)

数据集2

Words2 <- as.character(c("2016-08-30 15:05:01.55", "2016-08-30 15:10:01.56"))
Date <- as.POSIXct(Words2)
Speed <- c(0.385031168, 0.389179907)
Direction <- c(239.5721794,229.063366)
Data2 <- data.frame(Date, Speed, Direction)

合并的数据集应如下所示:

#    Date                     TagID   Xaxis     Yaxis     Zaxis      Speed     Direction
    # 1: 2016-08-30 15:04:51.97   2297.2  13.73827  14.67089  10.10618     NA        NA
    # 2: 2016-08-30 15:04:53.70   2297.2  13.76611  14.63040  10.19860     NA        NA
    # 3: 2016-08-30 15:04:54.25   2297.2  13.72899  14.68438  10.07538     NA        NA
    # 4: 2016-08-30 15:04:56.00   2297.2  13.70624  14.68586  10.05753     NA        NA
    # 5: 2016-08-30 15:04:56.54   2297.2  13.72280  14.69338  10.05484     NA        NA
    # 6: 2016-08-30 15:04:58.28   2297.2  13.69613  14.68652  10.04960     NA        NA
    # 7: 2016-08-30 15:04:58.84   2297.2  13.70763  14.69436  10.04295     NA        NA
    # 8: 2016-08-30 15:05:00.58   2297.2  13.68335  14.67780  10.05700     NA        NA
    # 9: 2016-08-30 15:05:01.15   2297.2  13.68846  14.68129  10.05404  0.385031  239.5722    
    # 10: 2016-08-30 15:05:02.89  2297.2  13.69010  14.68744  10.04391     NA        NA
    # 11: 2016-08-30 15:05:03.45  2297.2  13.67994  14.67547  10.05898     NA        NA
    # 12: 2016-08-30 15:05:05.19  2297.2  13.68067  14.67821  10.05447     NA        NA
    # 13: 2016-08-30 15:05:05.75  2297.2  13.68444  14.68190  10.05025     NA        NA
    # 14: 2016-08-30 15:05:07.49  2297.2  13.67648  14.67410  10.05917     NA        NA
    # 15: 2016-08-30 15:05:08.03  2297.2  13.67815  14.67574  10.05729     NA        NA

将数据帧转换为数据表并将它们合并在一起:

#Merge datasets
library(data.table)
Data1.dt <- data.table(Data1, key="Date")[,Date2:=Date]
Data2.dt <- data.table(Data2)
NewData <- Data1.dt[Data2.dt, list(Date=Date2, Speed, Direction), roll=-Inf][
  Data1.dt, list(Date, TagID, Xaxis, Yaxis, Zaxis, Speed, Direction)]
#does not work - error message but this worked in the other example

#Try again
Data1.dt <- data.table(Data1, key="Date")
Data2.dt <- data.table(Data2)
NewData2 <- Data1.dt[Data2.dt, on="Date", list(Date, Speed, Direction), roll=-Inf][
  Data1.dt, list(Date, TagID, Xaxis, Yaxis, Zaxis, Speed, Direction)]
#Merges but does not carry the data with it

我缺少什么使它合并两个数据集并随身携带数据? 请注意,发布原始示例的人遇到了类似问题,但问题是版本问题 - 我使用的是更高版本,所以它不应该成为问题。

R version 3.2.4 Revised (2016-03-16 r70336) -- "Very Secure Dishes"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

data.table version 1.10.4

提前感谢您的帮助。

0 个答案:

没有答案