In Mathematica there is the command Clip[x, {min, max}]
which gives x for min<=x<=max, min for x<min and and max for x>max, see
http://reference.wolfram.com/mathematica/ref/Clip.html (mirror)
What would be the fastest way to achieve this in R? Ideally it should be a function that is listable, and should ideally work on either a single value, vector, matrix or dataframe…
Rcpphasclampfor this:Here is a quick benchmark showing how it performs against other methods discussed :