Vectorized wrapper for taking the left N characters from a character, number, factor, etc. vector. Automatically trims leading whitespace. I find myself constantly trimming strings and grabbing only the first few characters so this saves some typing.
left(vec, n, trimws = TRUE, sameclass = FALSE)
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 |
sameclass | Should the output be the same class as the input? Defaults to |
left(vec = 'SomethingLong', n = 4)#> [1] "Some"left(vec = 40000.00, n = 2)#> [1] "40"left(vec = ' AnotherThing', n = 4, trimws = FALSE)#> [1] " An"left(vec = 400, 1, sameclass = TRUE)#> [1] 4