Vectorized wrapper for taking the right N characters from a character, number, factor, etc. vector. Automatically trims trailing whitespace. I find myself constantly trimming strings and grabbing only the last few characters so this saves some typing.

right(vec, n, trimws = TRUE, sameclass = FALSE)

Arguments

vec

A vector of any datatype. Not vectors of lists or matrices. I'm talking about charcter, numeric, logical, factor, etc.

n

The number of characters you want to keep.

trimws

Should the leading spaces be removed first? Default is TRUE.

sameclass

Should the output be the same class as the input? Defaults to FALSE. Generally, you don't want this (expecially for POSIX classes which is impossible to do). Should the return vector be the same class as the input vector? Default is FALSE (returns a character vector no matter the input).

See also

Examples

right(vec = 'SomethingLong', n = 4)
#> [1] "Long"
right(vec = 425575.4, n = 5)
#> [1] "575.4"
right(vec = 'AnotherThing ', n = 7, trimws = FALSE)
#> [1] "Thing "
right(vec = 401.98, 4, sameclass = TRUE)
#> [1] 1.98