Note that there are some explanatory texts on larger screens.

plurals
  1. POReplacing row values with the closest conditional values in R
    primarykey
    data
    text
    <p>In R, I have a problem that is similar to the one presented here: <a href="https://stackoverflow.com/questions/10077415/replacing-nas-in-r-with-nearest-value">Replacing NAs in R with nearest value</a>. The differences however, are that the values that I want to change are not NAs but any value less than 0, and also that changing these values is dependent on values in another column (so a conditional statement would need to be added). I'm having trouble understanding how to adapt some of the solutions presented in that question to my problem. It's also important that this be speedy as I have a lot of data.</p> <p>sample data</p> <pre><code>pred_trip &lt;- c(0,0,0,0,0,1,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1) locNumb &lt;- c(-1,-1,-1,-1,-1,2,2,2,2,3,3,0,0,0,4,4,4,4,-1,-1,-1,-1,-1,0,0,0,0,0,0,5,5,5,5) df &lt;- data.frame(pred_trip, locNumb) </code></pre> <p>So essentially if a value in the locNumb column is &lt;= 0 and there is a 0 in the pred_trip column then the value in the locNumb column gets reassigned to the closest value that is greater than 0. </p> <p>Desired output:</p> <pre><code> pred_trip locNumb 1 0 2 2 0 2 3 0 2 4 0 2 5 0 2 6 1 2 7 1 2 8 1 2 9 1 2 10 0 3 11 0 3 12 0 3 13 1 0 14 1 0 15 1 4 16 0 4 17 0 4 18 0 4 19 0 4 20 0 4 21 0 4 22 0 4 23 0 4 24 0 4 25 0 4 26 0 4 27 1 0 28 1 0 29 1 0 30 1 5 31 1 5 32 1 5 33 1 5 </code></pre> <p>I'm having trouble adapting the code in the similar solution as it relies a lot on is.na and doesn't include any of the other conditions that I need. But so in pseudo code something like: (not sure where to add in my other conditional statement of if pred_trip == 0.</p> <pre><code>f1 &lt;- function(df) { N &lt;- length(df) na.pos &lt;- which(df$locNumb &lt; 0 (df)) if (length(na.pos) %in% c(0, N)) { return(df) } non.na.pos &lt;- which(!df$locNumb &lt; 0(df)) intervals &lt;- findInterval(na.pos, non.na.pos, all.inside = TRUE) left.pos &lt;- non.na.pos[pmax(1, intervals)] right.pos &lt;- non.na.pos[pmin(N, intervals+1)] left.dist &lt;- na.pos - left.pos right.dist &lt;- right.pos - na.pos df[na.pos] &lt;- ifelse(left.dist &lt;= right.dist, df[left.pos], df[right.pos]) return(df) } </code></pre>
    singulars
    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