Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction for median similar to "which.max" and "which.min" / Extracting median rows from a data.frame
    text
    copied!<p>I occasionally need to extract specific rows from a data.frame based on values from one of the variables. <code>R</code> has built-in functions for maximum (<code>which.max()</code>) and minimum (<code>which.min()</code>) that allow me to easily extract those rows.</p> <p>Is there an equivalent for median? Or is my best bet to just write my own function?</p> <p>Here's an example data.frame and how I would use <code>which.max()</code> and <code>which.min()</code>:</p> <pre><code>set.seed(1) # so you can reproduce this example dat = data.frame(V1 = 1:10, V2 = rnorm(10), V3 = rnorm(10), V4 = sample(1:20, 10, replace=T)) # To return the first row, which contains the max value in V4 dat[which.max(dat$V4), ] # To return the seventh row, which contains the min value in V4 dat[which.min(dat$V4), ] </code></pre> <p>For this particular example, since there are an even number of observations, I would need to have two rows returned, in this case, rows 2 and 10.</p> <h2>Update</h2> <p>It would seem that there is not a built-in function for this. As such, using the <a href="https://stackoverflow.com/a/10256589/1270695">reply from Sacha</a> as a starting point, I wrote this function:</p> <pre><code>which.median = function(x) { if (length(x) %% 2 != 0) { which(x == median(x)) } else if (length(x) %% 2 == 0) { a = sort(x)[c(length(x)/2, length(x)/2+1)] c(which(x == a[1]), which(x == a[2])) } } </code></pre> <p>I'm able to use it as follows:</p> <pre><code># make one data.frame with an odd number of rows dat2 = dat[-10, ] # Median rows from 'dat' (even number of rows) and 'dat2' (odd number of rows) dat[which.median(dat$V4), ] dat2[which.median(dat2$V4), ] </code></pre> <p>Are there any suggestions to improve this?</p>
 

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