Question

So, I need to generate a spline function to feed it into another program which only accepts a fixed space between consecutive points. So, I used spline function in R with a given number of points to genrate spline, however, the floating-point cutoff makes the space among the points variable, for example:

spline(d$V1, d$V2, n=(max(d$V1)-min(d$V1))/0.0200)

> head(t.spl, 7)
      x      y
1 2.3000 -3.0204
2 2.3202 -3.0204
3 2.3404 -3.0204
4 2.3606 -3.0204
5 2.3807 -3.0204
6 2.4009 -3.0204
7 2.4211 -3.0204

so, the space between 1st 1nd 2nd row is 0.0202, while between 4th and 5th is 0.0201. So because of this problem, the other program that I am feeding this spline into, doesn't accept this. So, is there any way to make this work?

Was it helpful?

Solution 2

MathematicalCoffee is probably right. I'm just adding an alternative for the sake of wordiness.

myspline <- splinefun(dV$1,dV$2)

mydata.y <- myspline(desired_x_values,deriv=0)

Will guarantee the uniform x-spacings you desire.

OTHER TIPS

As an aside: please provide a reproducible example next time (I can't copy/paste your code in because I don't have d or t.spl)

I think you'll find that the different intervals (0.0202 vs 0.0201) is an artifact of the number of characters you are printing on the screen, not of the spline function.

It seems R is printing 4 digits after the decimal point for you for neatness, so it's doing the rounding only for the purposes of displaying the results to you. You can see how many digits are displayed with options('digits')$digits, and adjust it with options(digits=new_number_of_digits) (see ?options for details).

For example:

options(digits=4)
pi
# 3.142
options(digits=10)
pi
# 3.141592654

In summary, when you feed the values in to your other program, make sure you print the values with enough decimal points that the other program accepts the intervals as being "equal".

If you are writing to a file, for example, just make sure you write enough digits out. If you are copy-pasting from the R console, make sure you adjust R to print out enough digits.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top