How to extract initial, last, or middle characters from a string in R?

R ProgrammingServer Side ProgrammingProgramming

In Text analysis, we might want to extract characters from a single string or from a vector of strings. This extraction might be required to create a new string with some specific words required for further analysis. We can do this with the help of str_sub function of stringr package.

Example

Consider the below string −

> x1<-"Removing harmful things from the road is an act of charity"

Loading the stringr package −

> library(stringr)
> str_sub(x1,1,8)
[1] "Removing"
> str_sub(x1,1,23)
[1] "Removing harmful things"
> str_sub(x1,29,37)
[1] " the road"
> str_sub(x1,30,37)
[1] "the road"
> str_sub(x1,-58,-51)
[1] "Removing"
> str_sub(x1,-58,-1)
[1] "Removing harmful things from the road is an act of charity"
> str_sub(x1,-7,-1)
[1] "charity"
> str_sub(x1,-14,-1)
[1] "act of charity"
> str_sub(x1,-17,-1)
[1] "an act of charity"

Let’s have a look at extraction of number of characters for a vector of strings −

> x1<-c("Removing", "harmful", "things", "from", "the", "road", "is", "an", "act", "of", "charity")
> str_sub(x1,1,2)
[1] "Re"  "ha"  "th"  "fr"  "th"  "ro"  "is"  "an"  "ac"  "of"  "ch"
> str_sub(x1,1,3)
[1] "Rem" "har" "thi" "fro" "the" "roa" "is" "an" "act" "of" "cha"
> str_sub(x1,1,10)
[1] "Removing" "harmful" "things" "from" "the" "road"
[7] "is"  "an"   "act"    "of"    "charity"
> str_sub(x1,-7,-2)
[1] "emovin" "harmfu" "thing" "fro" "th" "roa" "i" "a"
[9] "ac" "o" "charit"
> str_sub(x1,-7,-1)
[1] "emoving" "harmful" "things" "from" "the" "road" "is"
[8] "an" "act" "of"     "charity"
> str_sub(x1,-10,-1)
[1] "Removing" "harmful" "things" "from" "the" "road"
[7] "is" "an" "act" "of" "charity"
raja
Published on 11-Aug-2020 07:46:02
Advertisements