Note that there are some explanatory texts on larger screens.

plurals
  1. POif else using the next unique value in column by key
    primarykey
    data
    text
    <p>I have a question. How do I perform the following operations on the sampdata object? The result that I would like to have is the sampres object.</p> <p>Thank you.</p> <p>Pseudocode:</p> <pre><code>if sampdata[,flow[1] == flow[2], by = site_no] sampdata[,flow[2]] = next unique flow value &amp; take the corresponding gage value # if yes no change # if no </code></pre> <p>Starting data:</p> <pre><code>library(data.table) sampdata &lt;- data.table(c(02446500,02446500,02446500,02467000,02467000,02467000,06818000,06818000,06818000,06818000,06893000,06893000,06893000,06893000,06934500,06934500,06934500,07010000,07010000,07010000,07289000,07289000,07289000),c(21,21,22,70,76,82,14700,14700,14700,14800,11000,11000,11000,11100,19400,19400,19500,32000,32000,32100,146000,146000,147000),c(4,4.01,4.02,73.05,73.06,73.07,1,1.01,1.02,1.03,1,1.01,1.02,1.03,-1.2,-1.19,-1.18,-9.02,-9.01,-9,-4.43,-4.42,-4.41)) setnames(sampdata,c("site_no", "flow", "gage")) setkey(sampdata, site_no) </code></pre> <p>End result:</p> <pre><code>sampres &lt;- data.table(c(02446500,02446500,02446500,02467000,02467000,02467000,06818000,06818000,06818000,06818000,06893000,06893000,06893000,06893000,06934500,06934500,06934500,07010000,07010000,07010000,07289000,07289000,07289000),c(21,22,22,70,76,82,14700,14800,14700,14800,11000,11100,11000,11100,19400,19500,19500,32000,32100,32100,146000,147000,147000),c(4,4.02,4.02,73.05,73.06,73.07,1,1.03,1.02,1.03,1,1.03,1.02,1.03,-1.2,-1.18,-1.18,-9.02,-9,-9,-4.43,-4.41,-4.41)) setnames(sampres,c("site_no", "newflow", "newgage")) setkey(sampres, site_no) </code></pre> <p>To clarify, here are the initial data and result side-by-side, from <code>cbind(sampdata,sampres)</code>:</p> <pre><code> site_no flow gage site_no flow gage 1: 2446500 21 4.00 2446500 21 4.00 2: 2446500 21 4.01 2446500 22 4.02 3: 2446500 22 4.02 2446500 22 4.02 4: 2467000 70 73.05 2467000 70 73.05 5: 2467000 76 73.06 2467000 76 73.06 6: 2467000 82 73.07 2467000 82 73.07 7: 6818000 14700 1.00 6818000 14700 1.00 8: 6818000 14700 1.01 6818000 14800 1.03 9: 6818000 14700 1.02 6818000 14700 1.02 10: 6818000 14800 1.03 6818000 14800 1.03 11: 6893000 11000 1.00 6893000 11000 1.00 12: 6893000 11000 1.01 6893000 11100 1.03 13: 6893000 11000 1.02 6893000 11000 1.02 14: 6893000 11100 1.03 6893000 11100 1.03 15: 6934500 19400 -1.20 6934500 19400 -1.20 16: 6934500 19400 -1.19 6934500 19500 -1.18 17: 6934500 19500 -1.18 6934500 19500 -1.18 18: 7010000 32000 -9.02 7010000 32000 -9.02 19: 7010000 32000 -9.01 7010000 32100 -9.00 20: 7010000 32100 -9.00 7010000 32100 -9.00 21: 7289000 146000 -4.43 7289000 146000 -4.43 22: 7289000 146000 -4.42 7289000 147000 -4.41 23: 7289000 147000 -4.41 7289000 147000 -4.41 site_no flow gage site_no flow gage </code></pre> <p>Thank you for the edits. <s>I have added bold to the flow/gage combinations that are the same.</s> The next unique flow value and the corresponding gage value need to become the 2nd newflow/newgage values. </p> <p>This is a small set of data that I'm trying to find an automated solution for because I made those changes by hand. I'm trying to determine an automated way of making the changes since I have several thousand sites that I need to work with. Thank you.</p>
    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.
    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