1

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"
TTR
  • 173
  • 1
  • 13
  • 1
    By second? by minute? by hour? Do you want a length 10 output? – Rich Scriven Nov 09 '15 at 19:32
  • @RichardScriven By amount: e.g if I want ten intermediate values then interpolate ten – TTR Nov 09 '15 at 20:11
  • Post some code, and a question related to that code. – Mike Wise Nov 09 '15 at 20:19
  • So my formulation of the question was not right, I don't want a step, I actually want to produce a certain amount of interpolated values. My coordinate interpolation uses approx, which takes n(number of values) as an option. I need something similar with time for consistency and so that things match. Otherwise if I interpolate 50 values between A and B and the time interpolation works with a step, then I will need extra math to make sure tB-tA/step=50. For example: I would interpolate ten pairs of values for the coordinates between A and B and ten values for the time between tA and tB. – TTR Nov 09 '15 at 20:22
  • Perhaps you could work on creating a [minimal](http://stackoverflow.com/help/mcve) and [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) question ... posting all of your code like that is a bit of an eye-bleed and will discourage many. – r2evans Nov 09 '15 at 20:58
  • In the meantime, will `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)` do something like what you want? (see `help(strptime)` for the percent-codes.) – r2evans Nov 09 '15 at 21:00
  • @r2evans It looks like that could be exactly what I want thank you! Will test and keep you posted – TTR Nov 09 '15 at 21:37
  • @r2evans It works perfectly! Thank you so much. Just one last thing, it adds " PST" at the end of each value, is there anyway I can avoid that? Otherwise that's fine I'll just substring of length-4 for each value. How do I upvote your comment? doesn't look like I can – TTR Nov 09 '15 at 22:03
  • @TaiTair If r2evan's answer helped you solve your problem, you can upvote it and accept it by clicking the arrow and checkmark on its left side. – Frank Nov 09 '15 at 22:29
  • And thanks to you I just accepted his answer.. Little new to this website. I already up-voted but apparently my reputation isn't high enough for it to be seen publicly. – TTR Nov 09 '15 at 22:32

1 Answers1

4
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.)

r2evans
  • 141,215
  • 6
  • 77
  • 149