First, the median_Rcpp function is defined to compute the median of the
given input vector. It is assumed that the input vector is unsorted, so a
copy of the input vector is made using clone and then std::nth_element
is used to access the nth sorted element. Since we only care about
accessing one sorted element of the vector for an odd length vector and two
sorted elements for an even length vector, it is faster to use
std::nth_element than either std::sort or std::partial_sort.
Next, the mad_rcpp function is defined to compute the median absolute
deviation. This is a simple one-liner thanks to the sugar function abs,
the vectorized operators, and the median_rcpp function defined above.
Note that a default value is specified for the scale_factor argument.