Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to substring every element in vector of strings?

Tags:

r

vector

I have the vector:

v <- c("godzilla", "jurassic", "googly")

I want the first 3 letters of every element in this vector. I would like to end up with:

# "god"   "jur"   "goo"

I have already tried using apply, but it didn't work. What should I do?

like image 980
user3749549 Avatar asked Jun 18 '14 17:06

user3749549


People also ask

How do I extract part of a string in R?

The substring function in R can be used either to extract parts of character strings, or to change the values of parts of character strings. substring of a vector or column in R can be extracted using substr() function. To extract the substring of the column in R we use functions like substr() and substring().

How do I get the first 4 characters of a string in R?

To get the first n characters from a string, we can use the built-in substr() function in R. The substr() function takes 3 arguments, the first one is a string, the second is start position, third is end position. Note: The negative values count backward from the last character.

How do I select a character from a string in R?

To get access to the individual characters in an R string, you need to use the substr function: str = 'string' substr(str, 1, 1) # This evaluates to 's'. For the same reason, you can't use length to find the number of characters in a string.

What does Substr do in R?

Substring() function in R is widely used to either extract the characters present in the data or to manipulate the data. You can easily extract the required characters from a string and also replace the values in a string.


2 Answers

One option is substring():

> substring(v, first = 1, last = 3)
[1] "god" "jur" "goo"

or also the R version, substr()

> substr(v, start = 1, stop = 3)
[1] "god" "jur" "goo"

Note the different names for the initial and last character you want.

As both of these functions are vectorised, there is no need for apply() and friends here.

like image 59
Gavin Simpson Avatar answered Oct 30 '22 01:10

Gavin Simpson


For the fun you can use a regular expression here :

sub('(^.{3}).*','\\1',v)
[1] "god" "jur" "goo"

Which is a another vectorized solution.

like image 25
agstudy Avatar answered Oct 29 '22 23:10

agstudy