TTR
TTR

Reputation: 183

R interpolate time

I am new to R but I am working on a project for which the back-end is in R.

I figured most of which I need. Like linear interpolation for cartographic degrees.

Problem is I need to interpolate time too now, because this is for a timed-animation where birds move from one point to another.

Let's say we have point A and point B. Time of access tA and tB.

I need to interpolate coordinates between A and B(Already figured how) with a dynamically calculated step, so for example if I want ten values, then it produces ten values.

And I need to interpolate times between tA and tB.

Basically I will be combining the two together in a file.

So now my question is how do I interpolate for example:

Between, 4-Mar-13 6:59:17
And, 4-Mar-13 12:51:13

Yes the format of the dates is going to be like so and it must take account of the date on top of the time.

How do I interpolate between those two date-time values in R?

Edit: Thanks to r2evans here is the answer to my own question, with extra conversion to work with ISO 8601 dates(which is required to generate CZML).

strftime(seq(as.POSIXct("4-Mar-13 6:50:17", format=fmt), as.POSIXct("4-Mar-13 12:51:13", format=fmt), len=10), format="%Y-%m-%dT%H:%M:%SZ", usetz=FALSE)
#[1] "2013-03-04T06:50:17Z" "2013-03-04T07:30:23Z" "2013-03-04T08:10:29Z"
#[4] "2013-03-04T08:50:35Z" "2013-03-04T09:30:41Z" "2013-03-04T10:10:48Z"
#[7] "2013-03-04T10:50:54Z" "2013-03-04T11:31:00Z" "2013-03-04T12:11:06Z"

Upvotes: 1

Views: 1359

Answers (1)

r2evans
r2evans

Reputation: 160437

fmt <- "%e-%b-%y %H:%M:%S"
seq(as.POSIXct("4-Mar-13 6:59:17", format=fmt), as.POSIXct("4-Mar-13 12:51:13", format=fmt), len=5)
## [1] "2013-03-04 06:59:17 PST" "2013-03-04 08:27:16 PST"
## [3] "2013-03-04 09:55:15 PST" "2013-03-04 11:23:14 PST"
## [5] "2013-03-04 12:51:13 PST"

If you need to change the timezone (PST), look for tz in the help.

If you need to change the output format, see help(format.POSIXct). (Our fmt variable works there, too.)

Upvotes: 4

Related Questions