How to remove last n characters from every element in the R vector How to remove last n characters from every element in the R vector r r

How to remove last n characters from every element in the R vector


Here is an example of what I would do. I hope it's what you're looking for.

char_array = c("foo_bar","bar_foo","apple","beer")a = data.frame("data"=char_array,"data2"=1:4)a$data = substr(a$data,1,nchar(a$data)-3)

a should now contain:

  data data21 foo_ 12 bar_ 23   ap 34    b 4


Here's a way with gsub:

cs <- c("foo_bar","bar_foo","apple","beer")gsub('.{3}$', '', cs)# [1] "foo_" "bar_" "ap"   "b"


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.