# 9.2: Median

- Page ID
- 8759

The median is the middle value after sorting the entire set of values. Let’s use the cleand-up `height_noNA`

variable created above to determine this for the NHANES height data. First we sort the data in order of their values:

`height_sorted <- sort(height_noNA)`

Next we find the median value. If there is an odd number of values in the list, then this is just the value in the middle, whereas if the number of values is even then we take the average of the two middle values. We can determine whether the number of items is even by dividing the length by two and seeing if there is a remainder; we do this using the `%%`

operator, which is known as the *modulus* and returns the remainder:

`5 %% 2`

`## [1] 1`

Here we will test whether the remainder is equal to one; if it is, then we will take the middle value, otherwise we will take the average of the two middle values. We can do this using an if/else structure, which executes different processes depending on which of the arguments are true:

```
if (logical value) {
functions to perform if logical value is true
} else {
functions to perform if logical value is false
}
```

Let’s do this with our data. To find the middle value when the number of items is odd, we will divide the length and then round up, using the `ceiling()`

function:

```
if (length(height_sorted) %% 2 == 1){
# length of vector is odd
median_height <- height_sorted[ceiling(length(height_sorted) / 2)]
} else {
median_height <- (height_sorted[length(height_sorted) / 2] +
height_sorted[1 + length(height_sorted) / (2)])/2
}
median_height
```

`## [1] 165`

We can compare this to the result from the built-in median function:

`median(height_noNA)`

`## [1] 165`