0

I have the following character vector of length 3:

test <- c("old_information", "old_car", "old_sting")

typeof(test) --> [1] "character"
length(test) --> [1] 3

I want to iterate through each element of test so it ultimately turns into the equivalent of:

test <- c("information", "car", "sting")

In other words: I would want to maintain the length of the character array but eliminate the same number of elements from each component (the 4 characters that spell 'old_' in front of each element.)

How could I do this?

xfrostyy
  • 37
  • 5

1 Answers1

1

I think you might actually want some regex replacement logic here:

test <- c("old_information", "old_car", "old_sting")
output <- sub("^.*?_", "", test)
output

[1] "information" "car"         "sting"

This would strip off all content from the start of each string up to, and including, the first underscore. If your logic really be to just strip off the first 5 characters, then just use substr:

output <- substr(test, 5, nchar(test))
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360