title: Change the median value by changing first 'n' values in dataframe R
body: <p>I have a dataframe as follows,</p>
  
  <pre><code>data = data.frame(name=letters, values = c(0.183487333,0.487690292, 0.510321455,0.616632727, 0.660789818, 0.845322255, 0.867910555, 0.911842909, 0.913312571, 1.017638286, 15.97454546, 16.20765382, 16.27542982, 16.307264, 17.38325527, 17.54139927, 70.08443928, 70.26106764, 75.79405382, 77.72412946, 79.58750724, 84.67373672, 87.04692364, 88.58626037, 94.79392582, 100.0000))
  data = data %&gt;% mutate(diff = abs(values - median(values)), rank_diff = dense_rank(diff)) %&gt;% arrange(diff) 
  </code></pre>
  
  <p>head(data)</p>
  
  <blockquote>
    <p>head(data)</p>
  </blockquote>
  
  <pre><code>  name   values       diff rank_diff
  1    n 16.30726 0.01591709         1
  2    m 16.27543 0.01591709         2
  3    l 16.20765 0.08369309         3
  4    k 15.97455 0.31680145         4
  5    o 17.38326 1.09190836         5
  6    p 17.54140 1.25005236         6
  
  &gt; median(data$values)
  [1] 16.29135
  </code></pre>
  
  <p>I have name and values column, and I have created diff column, by taking the absolute difference with the median value of values and then ranked it based on difference.</p>
  
  <p>The idea here is to change the median value to a new value, by taking the least number of values. Ideally I would like to have a function where I can pass the dataframe with new median value and number of rows to take. Once we pass the number of rows, the values should be changed equally in the first N rows, so that the median value of the entire column, becomes new median value.</p>
  
  <p>My function is as follows, data would be the dataframe, value would be the new median value and rows would be the number of rows. </p>
  
  <pre><code>fun &lt;- changemedian(data, value, rows) {
  }
  </code></pre>
  
  <p>Here suppose if I passing value as 16.5 and rows as 5, I want the values of the first 10 rows to change equally so that, the median value would be equal to 17. Since I have ranked the DF based on the absolute diff, each difference I make to the values, has some change in median value. I have tried to change .5 in first 5 values, to make it 16.5 and it is working in excel.</p>
  
  <p>I am trying to do this in R using various ways, without much success. Can anybody help me in doing this. Any sought of idea would help.</p>
  
  <p>Thanks</p>