- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove the first and last character in a string in R?
To remove the first and last character in a string, we can use str_sub function of stringr package. For example, if a word say tutorialspoint is mistakenly typed as ttutorialspointt and stored in a vector called x then to remove the first and last “t”, we can use the command str_sub(x,2,−2).
Example1
library(stringr) x1<−"aallworldpopulations" str_sub(x1,2,−2)
Output
[1] "allworldpopulation"
Example2
x2<−c("AAlabamaa", "AAlaskaa", "AAmerican Samoaa", "AArizonaa", "AArkansass", "CCaliforniaa", "CColoradoo", "CConnecticutt", "DDelawaree", "DDistrict of Columbiaa", "FFloridaa", "GGeorgiaa", "GGuamm", "HHawaiii", "IIdahoo", "IIllinoiss", "IIndianaa", "IIowaa", "KKansass", "KKentuckyy", "LLouisianaa", "MMainee", "MMarylandd", "MMassachusettss", "MMichigann", "MMinnesotaa", "MMinor Outlying Islandss", "MMississippii", "MMissourii", "MMontanaa", "NNebraskaa", "NNevadaa", "NNew Hampshiree", "NNew Jerseyy", "NNew Mexicoo", "NNew Yorkk", "NNorth Carolinaa", "NNorth Dakotaa", "NNorthern Mariana Islandss", "OOhioo", "OOklahomaa", "OOregonn", "PPennsylvaniaa", "PPuerto Ricoo", "RRhode Islandd", "SSouth Carolinaa", "SSouth Dakotaa", "TTennesseee", "TTexasx", "UU.S. Virgin Islandss", "UUtahh", "VVermontt", "VVirginiaa", "WWashingtonn", "WWest Virginiaa", "WWisconsinn", "WWyomingg") x2
Output
[1] "AAlabamaa" "AAlaskaa" [3] "AAmerican Samoaa" "AArizonaa" [5] "AArkansass" "CCaliforniaa" [7] "CColoradoo" "CConnecticutt" [9] "DDelawaree" "DDistrict of Columbiaa" [11] "FFloridaa" "GGeorgiaa" [13] "GGuamm" "HHawaiii" [15] "IIdahoo" "IIllinoiss" [17] "IIndianaa" "IIowaa" [19] "KKansass" "KKentuckyy" [21] "LLouisianaa" "MMainee" [23] "MMarylandd" "MMassachusettss" [25] "MMichigann" "MMinnesotaa" [27] "MMinor Outlying Islandss" "MMississippii" [29] "MMissourii" "MMontanaa" [31] "NNebraskaa" "NNevadaa" [33] "NNew Hampshiree" "NNew Jerseyy" [35] "NNew Mexicoo" "NNew Yorkk" [37] "NNorth Carolinaa" "NNorth Dakotaa" [39] "NNorthern Mariana Islandss" "OOhioo" [41] "OOklahomaa" "OOregonn" [43] "PPennsylvaniaa" "PPuerto Ricoo" [45] "RRhode Islandd" "SSouth Carolinaa" [47] "SSouth Dakotaa" "TTennesseee" [49] "TTexasx" "UU.S. Virgin Islandss" [51] "UUtahh" "VVermontt" [53] "VVirginiaa" "WWashingtonn" [55] "WWest Virginiaa" "WWisconsinn" [57] "WWyomingg"
Example
str_sub(x2,2,−2)
Output
[1] "Alabama" "Alaska" [3] "American Samoa" "Arizona" [5] "Arkansas" "California" [7] "Colorado" "Connecticut" [9] "Delaware" "District of Columbia" [11] "Florida" "Georgia" [13] "Guam" "Hawaii" [15] "Idaho" "Illinois" [17] "Indiana" "Iowa" [19] "Kansas" "Kentucky" [21] "Louisiana" "Maine" [23] "Maryland" "Massachusetts" [25] "Michigan" "Minnesota" [27] "Minor Outlying Islands" "Mississippi" [29] "Missouri" "Montana" [31] "Nebraska" "Nevada" [33] "New Hampshire" "New Jersey" [35] "New Mexico" "New York" [37] "North Carolina" "North Dakota" [39] "Northern Mariana Islands" "Ohio" [41] "Oklahoma" "Oregon" [43] "Pennsylvania" "Puerto Rico" [45] "Rhode Island" "South Carolina" [47] "South Dakota" "Tennessee" [49] "Texas" "U.S. Virgin Islands" [51] "Utah" "Vermont" [53] "Virginia" "Washington" [55] "West Virginia" "Wisconsin" [57] "Wyoming"
Example3
x3<−c("AAKK", "AALL", "AARR", "AASS", "AAZZ", "CCAA", "CCOO", "CCTT", "DCDC", "DDEE", "FFLL", "GGAA", "GGUU", "HHII", "IIAA", "IIDD", "IILL", "IINN", "KKSS", "KKYY", "LLAA", "MMAA", "MMDD", "MMEE", "MMII", "MMNN", "MMOO", "MMPP", "MMSS", "MMTT", "NNCC", "NNDD", "NNEE", "NNHH", "NNJJ", "NNMM", "NNVV", "NNYY", "OOHH", "OOKK", "OORR", "PPAA", "PPRR", "RRII", "SSCC", "SSDD", "TTNN", "TTXX", "UUMM", "UUTT", "VVAA", "VVII", "VVTT", "WWAA", "WWII", "WWVV", "WWYY") x3 [1] "AAKK" "AALL" "AARR" "AASS" "AAZZ" "CCAA" "CCOO" "CCTT" "DCDC" "DDEE" [11] "FFLL" "GGAA" "GGUU" "HHII" "IIAA" "IIDD" "IILL" "IINN" "KKSS" "KKYY" [21] "LLAA" "MMAA" "MMDD" "MMEE" "MMII" "MMNN" "MMOO" "MMPP" "MMSS" "MMTT" [31] "NNCC" "NNDD" "NNEE" "NNHH" "NNJJ" "NNMM" "NNVV" "NNYY" "OOHH" "OOKK" [41] "OORR" "PPAA" "PPRR" "RRII" "SSCC" "SSDD" "TTNN" "TTXX" "UUMM" "UUTT" [51] "VVAA" "VVII" "VVTT" "WWAA" "WWII" "WWVV" "WWYY" str_sub(x3,2,−2) [1] "AK" "AL" "AR" "AS" "AZ" "CA" "CO" "CT" "CD" "DE" "FL" "GA" "GU" "HI" "IA" [16] "ID" "IL" "IN" "KS" "KY" "LA" "MA" "MD" "ME" "MI" "MN" "MO" "MP" "MS" "MT" [31] "NC" "ND" "NE" "NH" "NJ" "NM" "NV" "NY" "OH" "OK" "OR" "PA" "PR" "RI" "SC" [46] "SD" "TN" "TX" "UM" "UT" "VA" "VI" "VT" "WA" "WI" "WV" "WY"
- Related Articles
- How to remove only last character from a string vector in R?
- How to remove the last character from a string in Java?
- How to remove the first character of string in PHP?
- Remove the last character from a string in the Swift language
- How to remove partial string after a special character in R?
- Remove all except the first character of a string in MySQL?
- How to remove first character from column name in R data frame?
- Python Program to Form a New String where the First Character and the Last Character have been Exchanged
- How to remove all text from a string before a particular character in R?
- How to remove a character (‘’) in string array and display result in one string php?
- How to find the first character of a string in C#?
- How to remove some last elements of a vector in R?
- How to remove a character in an R data frame column?
- How to cut only the first character in MySQL string?
- How to remove a particular character from a String.

Advertisements