0

I would like to take many values from interpolation at once. For example, from my data file('int.txt'), I have each "conc1" corresponding to each "depth1" (e.g., 1.1 m, 2.1 m, 3.1 m, 4.1 m, 5.1 m, 6.1 m).

Here, after interpolating my concentration data, I want to take "conc"s at "depth" of 1.2, 2.2, 3.2, 4.2, 5.2 m Following comments below (I'm editting my question), I made a code like this,

f = approxfun(depth1, conc1, rule=1,method='linear', xout=seq(1.2,5.2,1.0))

i<-approx(depth1, conc1, rule=1,method='linear', xout=seq(1.2,5.2,1.0))

It works well. Here, I have two more questions. 1. Then, how can I make two columns with data from i? Can I add these two columns to my data, 'int'? In this case, I will have no value at the last rows of the new columns. 2. I have one more x, y vector (y= conc2, x=depth2). I have each "conc2" at each "depth2", and "depth2" does not have regular intervals, so which is like 1.3, 2.7, 3.2... Here, after interpolating above, I want to extract all "conc1" values corresponding "depth2". Please let me know how to do these things. Thank you very much for your help :)

user2928318
  • 549
  • 3
  • 7
  • 20
  • Which method do you use for interpolation? polynoms, gam etc... – EDi Oct 28 '13 at 14:14
  • 1
    Hi and welcome to stackoverflow! You are much more likely to receive a helpful answer if you provide a [minimal, reproducible data set](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Also, show us the code of your attempted solutions, why they didn't work, and the expected results. Thanks! – Henrik Oct 28 '13 at 14:16
  • 1
    THank you for your comments. I aditted my question with some of data. Here, I used linear interpolation. Thank you for your comments/answer for my new question, in advance :) – user2928318 Nov 20 '13 at 10:10

1 Answers1

0

approxfun() generates a function that interpolates between given x and y vectors. You can call that function on a vector to take many approximations at once. There are several customizations you can make, (such as the simple method of interpolation and what to do outside of the data range,) but this should get you started until you specify the need for something more complicated.

?approxfun
f = approxfun(x=c(1.1, 2.1, 3.1, 4.1, 5.1),y=c(1, 3, 5, 2, 4),rule=1,method='constant')
plot(y=f(seq(1.1,5.1,.1)),x=seq(1.1,5.1,.1))
f = approxfun(x=c(1.1, 2.1, 3.1, 4.1, 5.1),y=c(1, 3, 5, 2, 4),rule=1,method='linear')
plot(y=f(seq(1.1,5.1,.1)),x=seq(1.1,5.1,.1))
bwarren2
  • 1,347
  • 1
  • 18
  • 36
  • Thanks a lot!! It helps a lot. I add some of data and made code following your comments. Would you answer my next qeustion above? thanks :) – user2928318 Nov 20 '13 at 10:11
  • For object creation, you can generate vectors (like in y=f(seq(1.1,5.1,.1)) ) and rbind or cbind them together, or make a matrix of your input values and apply the function to the matrix, or whatever. The benefit of approxfun() is that you can apply the function it returns to whatever you like. If I understand your last question correctly, you want to see what the interpolated results from your first depth~concentration map are like with the data from your second depth~concentration map. Just call the approxfun() from the first map on the inputs from the second. – bwarren2 Nov 20 '13 at 16:05
  • Thanks for your comments. I solved first part using cbind as you mentiond. But, I still cannot solve the last part. After interpolation of "conc", I want to extract "conc" values corresponding to "depth2". I tried approxfun() with depth1 insted of depth, but it did not work. Would you let me know to solve this? – user2928318 Nov 20 '13 at 17:11
  • I do not think I totally understand your question. I think you have two data sets, each with a depth and concentration. You can find any of the maps in those datasets by varying the arguments or order to approxfun. If you want the inverse of the depth~concentration map from group 2 to apply to group one, swap the argument order and call the result function on your other data set. If that is not sufficient, could you write up a minimal working example with a gap in the part you are missing? – bwarren2 Nov 20 '13 at 17:21
  • I don't think I have to use one more approxfun. Basically, I have interpolated "conc1" after interpolation. Then, I need to extract values from the interpolated "conc1" corresponding to "depth2". The interpolated "conc1" is infinite numbers, but "depth2" has several rows. So, I need find all values at depth 2 and extract corresponding conc from the interpolated "conc1". – user2928318 Nov 20 '13 at 18:02