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?
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().
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With