简体   繁体   English

R.从插值中获取值

[英]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). 例如,从我的数据文件('int.txt')中,我有对应于每个“ depth1”的每个“ conc1”(例如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, 在这里,在对浓度数据进行插值之后,我想在“深度”为1.2、2.2、3.2、4.2、5.2 m处采用“ conc”,并按照下面的注释(我正在编辑我的问题),编写了这样的代码,

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? 1.然后,如何使用来自i的数据创建两列? Can I add these two columns to my data, 'int'? 我可以将这两列添加到我的数据'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). 2.我还有一个x,y向量(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". 我在每个“ depth2”处都有每个“ conc2”,并且“ depth2”没有规则的间隔,所以就像1.3、2.7、3.2 ...在这里,在上面进行插值后,我想提取所有对应的“ conc1”值“ 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. roxfun()会生成一个在给定的x和y向量之间插值的函数。 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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