function approx interpolation of time series data sets

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

function approx interpolation of time series data sets

e-letter
Readers,

Am trying to use the function 'approx' to interpolate time series data sets:

data1:
01:23:40 5
01:23:45 10
01:23:50 12
01:23:55 7

data2:
01:23:42
01:23:47
01:23:51
01:23:54

The objective is to obtain interpolated values of 'data1' column 2 (5,
10, 12, 7) for the times shown in data2. Tried the following command
but received the error shown:

data3<-approx(data1,xout=data2)
Error in approx(data1, xout = data2) :
  (list) object cannot be coerced to type 'double'

What is my mistake please?

--
r2151

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: function approx interpolation of time series data sets

Rui Barradas
Hello,

Like this?


data1 <- read.table(text = "
01:23:40 5
01:23:45 10
01:23:50 12
01:23:55 7
")

data2 <- read.table(text = "
01:23:42
01:23:47
01:23:51
01:23:54
")

approx(as.POSIXct(data1$V1, format = "%H:%M:%S"), y = data1$V2, xout =
as.POSIXct(data2$V1, format = "%H:%M:%S"))


Hope this helps,

Rui Barradas
Em 16-01-2013 08:52, e-letter escreveu:

> Readers,
>
> Am trying to use the function 'approx' to interpolate time series data sets:
>
> data1:
> 01:23:40 5
> 01:23:45 10
> 01:23:50 12
> 01:23:55 7
>
> data2:
> 01:23:42
> 01:23:47
> 01:23:51
> 01:23:54
>
> The objective is to obtain interpolated values of 'data1' column 2 (5,
> 10, 12, 7) for the times shown in data2. Tried the following command
> but received the error shown:
>
> data3<-approx(data1,xout=data2)
> Error in approx(data1, xout = data2) :
>    (list) object cannot be coerced to type 'double'
>
> What is my mistake please?
>
> --
> r2151
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: function approx interpolation of time series data sets

e-letter
On 16/01/2013, Rui Barradas <[hidden email]> wrote:

> Hello,
>
> Like this?
>
>
> data1 <- read.table(text = "
> 01:23:40 5
> 01:23:45 10
> 01:23:50 12
> 01:23:55 7
> ")
>
> data2 <- read.table(text = "
> 01:23:42
> 01:23:47
> 01:23:51
> 01:23:54
> ")
>
> approx(as.POSIXct(data1$V1, format = "%H:%M:%S"), y = data1$V2, xout =
> as.POSIXct(data2$V1, format = "%H:%M:%S"))
>

Thanks. I have later realised that if data frames are unequal (i.e. data2edit

01:23:42
01:23:47
01:23:51
01:23:54
01:23:58
01:23:59

the result for 'y' is

$y
[1] NA NA NA NA NA NA

Similar error occurs with the 'zoo' package.

Is it better to do interpolation/further manipulation in another software tool?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: function approx interpolation of time series data sets

Gabor Grothendieck
On Fri, Jan 18, 2013 at 7:31 AM, e-letter <[hidden email]> wrote:

> On 16/01/2013, Rui Barradas <[hidden email]> wrote:
>> Hello,
>>
>> Like this?
>>
>>
>> data1 <- read.table(text = "
>> 01:23:40 5
>> 01:23:45 10
>> 01:23:50 12
>> 01:23:55 7
>> ")
>>
>> data2 <- read.table(text = "
>> 01:23:42
>> 01:23:47
>> 01:23:51
>> 01:23:54
>> ")
>>
>> approx(as.POSIXct(data1$V1, format = "%H:%M:%S"), y = data1$V2, xout =
>> as.POSIXct(data2$V1, format = "%H:%M:%S"))
>>
>
> Thanks. I have later realised that if data frames are unequal (i.e. data2edit
>
> 01:23:42
> 01:23:47
> 01:23:51
> 01:23:54
> 01:23:58
> 01:23:59
>
> the result for 'y' is
>
> $y
> [1] NA NA NA NA NA NA
>
> Similar error occurs with the 'zoo' package.


its not clear precisely what you tried with zoo but have a look at this:

> data1 <- "
+  01:23:40 5
+  01:23:45 10
+  01:23:50 12
+  01:23:55 7"
>
> data2 <- "
+  01:23:42
+  01:23:47
+  01:23:51
+  01:23:54"
>
> library(zoo)
> library(chron)
> z1 <- read.zoo(text = data1, FUN = times)
> z2 <- read.zoo(text = data2, FUN = times)
> na.approx(z1, xout = time(z2))
01:23:42 01:23:47 01:23:51 01:23:54
     7.0     10.8     11.0      8.0

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: function approx interpolation of time series data sets

e-letter
On 18/01/2013, Gabor Grothendieck <[hidden email]> wrote:

> On Fri, Jan 18, 2013 at 7:31 AM, e-letter <[hidden email]> wrote:
>> On 16/01/2013, Rui Barradas <[hidden email]> wrote:
>>> Hello,
>>>
>>> Like this?
>>>
>>>
>>> data1 <- read.table(text = "
>>> 01:23:40 5
>>> 01:23:45 10
>>> 01:23:50 12
>>> 01:23:55 7
>>> ")
>>>
>>> data2 <- read.table(text = "
>>> 01:23:42
>>> 01:23:47
>>> 01:23:51
>>> 01:23:54
>>> ")
>>>
>>> approx(as.POSIXct(data1$V1, format = "%H:%M:%S"), y = data1$V2, xout =
>>> as.POSIXct(data2$V1, format = "%H:%M:%S"))
>>>
>>
>> Thanks. I have later realised that if data frames are unequal (i.e.
>> data2edit
>>
>> 01:23:42
>> 01:23:47
>> 01:23:51
>> 01:23:54
>> 01:23:58
>> 01:23:59
>>
>> the result for 'y' is
>>
>> $y
>> [1] NA NA NA NA NA NA
>>
>> Similar error occurs with the 'zoo' package.
>
>
> its not clear precisely what you tried with zoo but have a look at this:
>
>> data1 <- "
> +  01:23:40 5
> +  01:23:45 10
> +  01:23:50 12
> +  01:23:55 7"
>>
>> data2 <- "
> +  01:23:42
> +  01:23:47
> +  01:23:51
> +  01:23:54"
>>
>> library(zoo)
>> library(chron)
>> z1 <- read.zoo(text = data1, FUN = times)
>> z2 <- read.zoo(text = data2, FUN = times)
>> na.approx(z1, xout = time(z2))
> 01:23:42 01:23:47 01:23:51 01:23:54
>      7.0     10.8     11.0      8.0
>

With respect to zoo package, looks like I tried to apply zoo package
to the vectors directly, not converting them to zoo objects using
'read.zoo ...'.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: function approx interpolation of time series data sets

e-letter
In reply to this post by Gabor Grothendieck
On 18/01/2013, Gabor Grothendieck <[hidden email]> wrote:

> On Fri, Jan 18, 2013 at 7:31 AM, e-letter <[hidden email]> wrote:
>> On 16/01/2013, Rui Barradas <[hidden email]> wrote:
>>> Hello,
>>>
>>> Like this?
>>>
>>>
>>> data1 <- read.table(text = "
>>> 01:23:40 5
>>> 01:23:45 10
>>> 01:23:50 12
>>> 01:23:55 7
>>> ")
>>>
>>> data2 <- read.table(text = "
>>> 01:23:42
>>> 01:23:47
>>> 01:23:51
>>> 01:23:54
>>> ")
>>>
>>> approx(as.POSIXct(data1$V1, format = "%H:%M:%S"), y = data1$V2, xout =
>>> as.POSIXct(data2$V1, format = "%H:%M:%S"))
>>>
>>
>> Thanks. I have later realised that if data frames are unequal (i.e.
>> data2edit
>>
>> 01:23:42
>> 01:23:47
>> 01:23:51
>> 01:23:54
>> 01:23:58
>> 01:23:59
>>
>> the result for 'y' is
>>
>> $y
>> [1] NA NA NA NA NA NA
>>
>> Similar error occurs with the 'zoo' package.
>
>
> its not clear precisely what you tried with zoo but have a look at this:
>
>> data1 <- "
> +  01:23:40 5
> +  01:23:45 10
> +  01:23:50 12
> +  01:23:55 7"
>>
>> data2 <- "
> +  01:23:42
> +  01:23:47
> +  01:23:51
> +  01:23:54"
>>
>> library(zoo)
>> library(chron)
>> z1 <- read.zoo(text = data1, FUN = times)
>> z2 <- read.zoo(text = data2, FUN = times)
>> na.approx(z1, xout = time(z2))
> 01:23:42 01:23:47 01:23:51 01:23:54
>      7.0     10.8     11.0      8.0
>

Latest error:

> data1
        V1 V2
1 01:23:40  5
2 01:23:45 10
3 01:23:50 12
4 01:23:55  7

> data2
        V1
1 01:23:42
2 01:23:47
3 01:23:51
4 01:23:54
5 01:23:58
6 01:23:59

> data1zoo<-read.zoo(text=data1,FUN=times)
Error in textConnection(text) : invalid 'text' argument

> data2zoo<-read.zoo(text=data2,FUN=times)
Error in textConnection(text) : invalid 'text' argument

Then I tried to create objects differently:

data1zoo<-read.zoo('test1.txt',FUN=times)
> data1zoo
01:23:40 01:23:45 01:23:50 01:23:55
       5       10       12        7

data2zoo<-read.zoo('test2.txt',FUN=times)
> data2zoo

01:23:42
01:23:47
01:23:51
01:23:54
01:23:58
01:23:59

> data3<-(na.approx(merge(data1zoo,data2zoo),time(data1zoo)))
Error in na.approx.default(object, x = x, xout = xout, na.rm = FALSE,  :
  x and index must have the same length

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: function approx interpolation of time series data sets

Rui Barradas
Hello,

Both Gabor's and my way work and produce the same results:


#-- Gabor
library(zoo)
library(chron)

data1 <- "
01:23:40  5
01:23:45 10
01:23:50 12
01:23:55  7"

data2 <- "
01:23:42
01:23:47
01:23:51
01:23:54
01:23:58
01:23:59"

data1zoo <- read.zoo(text=data1, FUN=times)
data2zoo <- read.zoo(text=data2, FUN=times)
na.approx(data1zoo, xout = time(data2zoo))

#-- Rui

data1 <- read.table(text = "
01:23:40 5
01:23:45 10
01:23:50 12
01:23:55 7
")

data2 <- read.table(text = "
01:23:42
01:23:47
01:23:51
01:23:54
01:23:58
01:23:59
")

approx(as.POSIXct(data1$V1, format = "%H:%M:%S"),
        y = data1$V2,
        xout = as.POSIXct(data2$V1, format = "%H:%M:%S"))


Note that the last two values of data2 are outside the range of data1,
and therefore the interpolation functions return nothing
(zoo::na.approx) or NA (stats::approx)

Hope this helps,

Rui Barradas

Em 18-01-2013 13:51, e-letter escreveu:

> On 18/01/2013, Gabor Grothendieck <[hidden email]> wrote:
>> On Fri, Jan 18, 2013 at 7:31 AM, e-letter <[hidden email]> wrote:
>>> On 16/01/2013, Rui Barradas <[hidden email]> wrote:
>>>> Hello,
>>>>
>>>> Like this?
>>>>
>>>>
>>>> data1 <- read.table(text = "
>>>> 01:23:40 5
>>>> 01:23:45 10
>>>> 01:23:50 12
>>>> 01:23:55 7
>>>> ")
>>>>
>>>> data2 <- read.table(text = "
>>>> 01:23:42
>>>> 01:23:47
>>>> 01:23:51
>>>> 01:23:54
>>>> ")
>>>>
>>>> approx(as.POSIXct(data1$V1, format = "%H:%M:%S"), y = data1$V2, xout =
>>>> as.POSIXct(data2$V1, format = "%H:%M:%S"))
>>>>
>>>
>>> Thanks. I have later realised that if data frames are unequal (i.e.
>>> data2edit
>>>
>>> 01:23:42
>>> 01:23:47
>>> 01:23:51
>>> 01:23:54
>>> 01:23:58
>>> 01:23:59
>>>
>>> the result for 'y' is
>>>
>>> $y
>>> [1] NA NA NA NA NA NA
>>>
>>> Similar error occurs with the 'zoo' package.
>>
>>
>> its not clear precisely what you tried with zoo but have a look at this:
>>
>>> data1 <- "
>> +  01:23:40 5
>> +  01:23:45 10
>> +  01:23:50 12
>> +  01:23:55 7"
>>>
>>> data2 <- "
>> +  01:23:42
>> +  01:23:47
>> +  01:23:51
>> +  01:23:54"
>>>
>>> library(zoo)
>>> library(chron)
>>> z1 <- read.zoo(text = data1, FUN = times)
>>> z2 <- read.zoo(text = data2, FUN = times)
>>> na.approx(z1, xout = time(z2))
>> 01:23:42 01:23:47 01:23:51 01:23:54
>>       7.0     10.8     11.0      8.0
>>
>
> Latest error:
>
>> data1
>          V1 V2
> 1 01:23:40  5
> 2 01:23:45 10
> 3 01:23:50 12
> 4 01:23:55  7
>
>> data2
>          V1
> 1 01:23:42
> 2 01:23:47
> 3 01:23:51
> 4 01:23:54
> 5 01:23:58
> 6 01:23:59
>
>> data1zoo<-read.zoo(text=data1,FUN=times)
> Error in textConnection(text) : invalid 'text' argument
>
>> data2zoo<-read.zoo(text=data2,FUN=times)
> Error in textConnection(text) : invalid 'text' argument
>
> Then I tried to create objects differently:
>
> data1zoo<-read.zoo('test1.txt',FUN=times)
>> data1zoo
> 01:23:40 01:23:45 01:23:50 01:23:55
>         5       10       12        7
>
> data2zoo<-read.zoo('test2.txt',FUN=times)
>> data2zoo
>
> 01:23:42
> 01:23:47
> 01:23:51
> 01:23:54
> 01:23:58
> 01:23:59
>
>> data3<-(na.approx(merge(data1zoo,data2zoo),time(data1zoo)))
> Error in na.approx.default(object, x = x, xout = xout, na.rm = FALSE,  :
>    x and index must have the same length
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.