Ross Bennett — written Jan 1, 2013 — source
The R functions head
and tail
return the first (last) n elements
of the input vector. With Rcpp sugar, the functions head
and tail
work the same way as they do in R.
Here we use std::sort
from the STL and then tail
to return the top
n items (items with the highest values) of the input vector.
A simple illustration:
[1] 1.37096 -0.56470 0.36313 0.63286 0.40427 -0.10612 1.51152 [8] -0.09466 2.01842 -0.06271
[1] 1.371 1.512 2.018
Here we use std::sort
from the STL and then head
to return the bottom
n items (items with the lowest values) of the input vector.
[1] -0.56470 -0.10612 -0.09466Tweet