Skip to contents

sort_uniq

Here are a few functions that I am creating as shorthands for some of commonly used expressions/combined functions I keep coming back to. The idea is to read this package and its functions into any random project I’m working on so I can have these shorthands, handy (even if they don’t seem very R-like).

vec <- sample(1:10, size = 20, replace=TRUE)

vec
#>  [1] 7 5 6 4 6 9 5 5 8 2 7 5 5 2 6 4 2 3 9 6

sort_uniq(vec)
#> [1] 2 3 4 5 6 7 8 9

A funny thing I ended up realizing when trying to write a “simple test”:

There’s a tiny probability that this could fail randomly and it wouldn’t even be wrong! So the lesson here is don’t write tests like mine.

len_uniq

vec <- sample(1:10, size = 20, replace=TRUE)

vec
#>  [1]  7  5  6  9  6 10  5  2  1  4  8  6  1  6  2  3  1  9  2  5

sort_uniq(vec)
#>  [1]  1  2  3  4  5  6  7  8  9 10

not_in

And finally, an actually personally irritating one of mine: NOT %in%. The fact that we can’t put in a !%in% or not %in% like in Python is one example where I can actually say Python did it better than R here.

#' \dontrun{
#'     not_in(1, 2:10)
#' }