More obvious than substring
for me would be strtrim
:
> x <- c("hi", "hello", "Nyarlathotep") > x [1] "hi" "hello" "Nyarlathotep" > strtrim(x, 5) [1] "hi" "hello" "Nyarl"
substring
great for extracting data from a string at a given position, but strtrim
does exactly what you are looking for.
The second argument, widths
, can be a width vector of the same length as the input vector, in which case each element can be cut off by a certain amount.
> strtrim(x, c(1, 2, 3)) [1] "h" "he" "Nya"
source share