简体   繁体   English

R-将时间序列插值到相同的时间间隔但有新的时间点

[英]R - interpolate time series to same interval but new time points

Apologies if this is an obvious question, but I am new to R, having spent many years with SAS 道歉,如果这是一个明显的问题,但我是R的新手,已经在SAS工作了很多年

I have two data files of measurements taken on 10-second intervals, but not at the exact same time points. 我有两个以10秒为间隔进行测量的数据文件,但不是在完全相同的时间点进行。 I would like to convert one of the time series to match the times of the other, adjusting its values with linear interpolation. 我想转换一个时间序列以匹配另一个时间,并使用线性插值来调整其值。 In SAS I could do this pretty quickly with proc expand, but I can't find anything similar package in R (I've looked at zoo and xts). 在SAS中,我可以使用proc expand很快完成此操作,但是我在R中找不到任何类似的软件包(我看过zoo和xts)。

To show what I mean, here are snippets of my two files. 为了说明我的意思,以下是我的两个文件的摘要。 In this case one time series is on the 3's and the other is on the 2's. 在这种情况下,一个时间序列在3上,另一个在2上。 In this particular case that 1-second difference is probably trivial, but this is a problem I run into a lot in my work and so I'd like to know if there's any easy way to recalculate, via linear interpolation, the values in the second data set to also be on the 3's 在这种特殊情况下,一秒的差异可能微不足道,但这是我在工作中遇到的一个问题,因此我想知道是否有任何简便的方法可以通过线性插值法重新计算第二个数据集也位于3的位置

Date,Time,Value 日期,时间,价值

3-Nov-16,13:15:53,264.651 11月3日 - 16,13:15:53,264.651

3-Nov-16,13:16:03,264.58 3-NOV-16,13:16:03,264.58

3-Nov-16,13:16:13,264.368 3-NOV-16,13:16:13,264.368

3-Nov-16,13:16:23,264.273 3-NOV-16,13:16:23,264.273

3-Nov-16,13:16:33,264.391 3-NOV-16,13:16:33,264.391

11/3/16,1:15:52 PM,10.1 11/3 / 16,1:15:52 PM,10.1

11/3/16,1:16:02 PM,10.1 11/3 / 16,1:16:02 PM,10.1

11/3/16,1:16:12 PM,10.1 11/3 / 16,1:16:12 PM,10.1

11/3/16,1:16:22 PM,10.1 11/3 / 16,1:16:22 PM,10.1

11/3/16,1:16:32 PM,10.1 11/3 / 16,1:16:32 PM,10.1

You can use the 'approx' function. 您可以使用“大约”功能。 Here is an example with your data: 这是您的数据的示例:

> input <- read.table(text = "11/3/16,1:15:52 PM,10.1
+ 
+ 11/3/16,1:16:02 PM,10.1
+ 
+ 11/3/16,1:16:12 PM,10.1
+ 
+ 11/3/16,1:16:22 PM,10.1
+ 
+ 11/3/16,1:16:32 PM,10.1", as.is = TRUE, sep = ',')
> 
> # convert the date
> input$time <- as.POSIXct(input$V2, format = "%I:%M:%S %p")
> library(lubridate)
> 
> input$newtime <- input$time
> 
> first <- read.table(text = "3-Nov-16,13:15:53,264.651
+ 
+ 3-Nov-16,13:16:03,264.58
+ 
+ 3-Nov-16,13:16:13,264.368
+ 
+ 3-Nov-16,13:16:23,264.273
+ 
+ 3-Nov-16,13:16:33,264.391", as.is = TRUE, sep = ',')
> first$time <- as.POSIXct(first$V2, format = "%H:%M:%S")
> 
> # use "approx" to interprete values
> # find values for times in "input" since "first" has different values
> input$result <- approx(first$time,
+                        first$V3,
+                        xout = input$time,
+                        rule = 2
+                        )$y
>                        
> 
> input
       V1         V2   V3                time             newtime   result
1 11/3/16 1:15:52 PM 10.1 2017-01-11 13:15:52 2017-01-11 13:15:52 264.6510
2 11/3/16 1:16:02 PM 10.1 2017-01-11 13:16:02 2017-01-11 13:16:02 264.5871
3 11/3/16 1:16:12 PM 10.1 2017-01-11 13:16:12 2017-01-11 13:16:12 264.3892
4 11/3/16 1:16:22 PM 10.1 2017-01-11 13:16:22 2017-01-11 13:16:22 264.2825
5 11/3/16 1:16:32 PM 10.1 2017-01-11 13:16:32 2017-01-11 13:16:32 264.3792
> first
        V1       V2      V3                time
1 3-Nov-16 13:15:53 264.651 2017-01-11 13:15:53
2 3-Nov-16 13:16:03 264.580 2017-01-11 13:16:03
3 3-Nov-16 13:16:13 264.368 2017-01-11 13:16:13
4 3-Nov-16 13:16:23 264.273 2017-01-11 13:16:23
5 3-Nov-16 13:16:33 264.391 2017-01-11 13:16:33
> 

I apologize that I am bit lazy trying evaluating input as you asked. 抱歉,我懒于尝试按您的要求评估输入。 I am still learning R. I wonder if this piece of code solves your fundamental issue. 我仍在学习R。我想知道这段代码是否可以解决您的基本问题。

The algorithm is simple 算法很简单

I change all time to Unix time which is the number of seconds since epoch. 我将所有时间都更改为Unix时间,这是自新纪元以来的秒数。

I use unixtime as x and the second number as y 我将unixtime用作x,将第二个数字用作y

I create new data points based on the second set of datetime as unixtime. 我基于第二组日期时间作为unixtime创建新的数据点。

toUnixTime <- function( dateobj ){
    return (as.numeric(as.POSIXct(dateobj, origin="1970-01-01")))
}

toDateTime <- function( unixtime){
return (as.POSIXct(unixtime, origin="1970-01-01"))
}
toUnix <- function( datetime ){
return ( as.numeric(strptime( datetime, "%d-%b-%y,%H:%M:%S") ) )
}
toUnix2 <- function( datetime ){
return ( as.numeric(strptime( datetime, "%m/%d/%y,%I:%M:%S %p") ) )
}
main <- function(){
x <- c(toUnix("3-Nov-16,13:15:53" ),toUnix("3-Nov-16,13:16:03:53"))
y <- c(264.651,264.58)
f <- approxfun(x,y)
f(toUnix2("11/3/16,1:16:02 PM"))
}
main()

This outputs 这个输出

264.5871 for 11/3/16,1:16:02 PM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM