简体   繁体   中英

How to substring every element in vector of strings?

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?

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.

For the fun you can use a regular expression here :

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

Which is a another vectorized solution.

@Gavin Simpson's answer is the right way to go, but if you want to use apply() and friends here, you can try the following:

> sapply(strsplit(v, ""), function(x) paste0(x[1:3], collapse=""))
[1] "god" "jur" "goo"

A stringr option is str_sub :

str_sub(v, 1, 3)
#[1] "god" "jur" "goo"

And str_sub_all for multiple substrings in each string:

str_sub_all(v, c(1, 2), c(3, 4))
# [[1]]
# [1] "god" "odz"
# 
# [[2]]
# [1] "jur" "ura"
# 
# [[3]]
# [1] "goo" "oog"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM