Note that there are some explanatory texts on larger screens.

plurals
  1. POinterpolation of grouped data using data.table
    primarykey
    data
    text
    <p>This is a continuation of a question that I had originally posted at <a href="http://r.789695.n4.nabble.com/subset-between-data-table-list-and-single-data-table-object-tp4673202.html" rel="nofollow">http://r.789695.n4.nabble.com/subset-between-data-table-list-and-single-data-table-object-tp4673202.html</a> . Matthew had suggested that I post my question here so I am doing that now.</p> <p>This is my input below:</p> <pre><code>library(data.table) library(pracma) # for the interp1 function tempbigdata1 &lt;- data.table(c(14.80, 14.81, 14.82), c(7900, 7920, 7930), c("02437100", "02437100", "02437100")) tempbigdata2 &lt;- data.table(c(9.98, 9.99, 10.00), c(816, 819, 821), c("02446500", "02446500", "02446500")) tempbigdata3 &lt;- data.table(c(75.65, 75.66, 75.67), c(23600, 23700, 23800), c("02467000", "02467000", "02467000")) tempsbigdata &lt;- rbind(tempbigdata1, tempbigdata2, tempbigdata3) setnames(tempsbigdata,c("y", "x", "site_no")) setkey(tempsbigdata, site_no) tempsbigdata y x site_no 1: 14.80 7900 02437100 2: 14.81 7920 02437100 3: 14.82 7930 02437100 4: 9.98 816 02446500 5: 9.99 819 02446500 6: 10.00 821 02446500 7: 75.65 23600 02467000 8: 75.66 23700 02467000 9: 75.67 23800 02467000 aimsmall &lt;- data.table(c("02437100", "02446500", "02467000"), c(3882.65, 819.82, 23742.37), c(1830.0, 382.0, 10400.0)) setnames(aimsmall,c("site_no", "mean", "p50")) setkey(aimsmall, site_no) aimsmall site_no mean p50 1: 02437100 3882.65 1830 2: 02446500 819.82 382 3: 02467000 23742.37 10400 </code></pre> <p>I am using this code to generate the interpolated <code>tempsbigdata$y</code> using the <code>aimsmall$mean</code> values by the <code>site_no</code>:</p> <pre><code> meanpre &lt;- tempsbigdata[,if(aimsmall$mean &gt; min(tempsbigdata$x){ interp1(tempsbigdata$x, tempsbigdata$y, xi = aimsmall$mean, method ="linear")},by=site_no] </code></pre> <p>This is the output from the function <code>meanpre</code>, but it is not correct.<br /></p> <pre><code>meanpre site_no V1 1: 02437100 12.07599 2: 02437100 9.99410 3: 02437100 19.56813 4: 02446500 12.07599 5: 02446500 9.99410 6: 02446500 19.56813 7: 02467000 12.07599 8: 02467000 9.99410 9: 02467000 19.56813 </code></pre> <p>This is what I would like to get:</p> <pre><code>meanpre site_no V1 1: 02446500 9.99 2: 02467000 75.66 </code></pre> <p>Any suggestions? Thank you.</p> <p><strong>UPDATE 1:</strong></p> <p>Hugh, I used the approx function in the past and it is not accurate for my data; however, the <code>interp1</code> function in <code>pracma</code> is accurate. The <code>mean</code> and <code>p50</code> columns in <code>aimsmall</code> &amp; the <code>x</code> values in <code>tempsbigdata</code> are discharge values. The <code>y</code> in <code>tempsbigdata</code> represent gage heights. I am using the <code>interp1</code> function to determine the appropriate gage height or <code>y</code> value for the discharge values or <code>mean</code> (and <code>p50</code>).</p> <p>Frank, thank you for your advice and suggested code. This is the output for your suggested code:</p> <pre><code>tempsbigdata[aimsmall][,if(mean[1] &gt; min(x)){interp1(tempsbigdata$x,tempsbigdata$y, xi = aimsmall$mean, method ="linear")},by=site_no] site_no V1 1: 02446500 12.07599 2: 02446500 9.99410 3: 02446500 75.66424 4: 02467000 12.07599 5: 02467000 9.99410 6: 02467000 75.66424 </code></pre> <p>When I run the following code I get the result below:</p> <pre><code> interp1(tempsbigdata$x, tempsbigdata$y, xi = aimsmall$mean, method ="linear") [1] 12.07599 9.99410 75.66424 </code></pre> <p>Is there any way to get this in return? Thank you.</p> <pre><code> site_no V1 1: 02446500 9.99 2: 02467000 75.66 </code></pre> <p><strong>UPDATE 2</strong></p> <p>Frank, thank you and I have added the code to make it easier to have the data in R. Pracma is an R package of numerical method routines that were ported from GNU Octave [similar to MATLAB(R)] to R. The <code>interp1</code> function is a one-dimensional interpolation function.</p> <p>Frank, that was perfect (your last comment about the R code for "do stuff"):</p> <pre><code> tempsbigdata[aimsmall][,if(mean[1] &gt; min(x)){interp1(x, y, xi = mean[1], method ="linear")},by=site_no] site_no V1 1: 02446500 9.99410 2: 02467000 75.66424 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload