How to remove last n characters from every element in the R vector
Although this is mostly the same with the answer by @nfmcclure, I prefer using stringr
package as it provdies a set of functions whose names are most consistent and descriptive than those in base R (in fact I always google for "how to get the number of characters in R" as I can't remember the name nchar()
).
library(stringr)str_sub(iris$Species, end=-4)#or str_sub(iris$Species, 1, str_length(iris$Species)-3)
This removes the last 3 characters from each value at Species
column.