简体   繁体   中英

R. Taking values from interpolation

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" (eg, 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 :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM