
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Last Character from a String Vector in R
Sometimes the string vector contains unnecessary characters at the end or at the starting and do not make sense, it is also possible that the string makes sense but nor required there is a spelling mistake. In such type of cases, we need to remove the unnecessary characters. This can be done by using gsub function.
Example1
x1<−"Tutorialspoint is an E-learning website and one of the biggest websites in the worlds" gsub('.{1}$','',x1)
Output
[1] "Tutorialspoint is an E-learning website and one of the biggest websites in the world"
Example2
x2<−c("Alabamaa", "Alaskaa", "American Samoaa", "Arizonaa", "Arkansass", "Californiaa", "Coloradoo", "Connecticutt", "Delawaree", "District of Columbiaa", "Floridaa", "Georgiaa", "Guamm", "Hawaiii", "Idahoo", "Illinoiss", "Indianaa", "Iowaa", "Kansass", "Kentuckyy", "Louisianaa", "Mainee", "Marylandd", "Massachusettss", "Michigann", "Minnesotaa", "Minor Outlying Islandss", "Mississippii", "Missourii", "Montanaa", "Nebraskaa", "Nevadaa", "New Hampshiree", "New Jerseyy", "New Mexicoo", "New Yorkk", "North Carolinaa", "North Dakotaa", "Northern Mariana Islandss", "Ohioo", "Oklahomaa", "Oregonn", "Pennsylvaniaa", "Puerto Ricoo", "Rhode Islandd", "South Carolinaa", "South Dakotaa", "Tennesseee", "Texasx", "U.S. Virgin Islandss", "Utahh", "Vermontt", "Virginiaa", "Washingtonn", "West Virginiaa", "Wisconsinn", "Wyomingg") x2
Output
[1] "Alabamaa" "Alaskaa" [3] "American Samoaa" "Arizonaa" [5] "Arkansass" "Californiaa" [7] "Coloradoo" "Connecticutt" [9] "Delawaree" "District of Columbiaa" [11] "Floridaa" "Georgiaa" [13] "Guamm" "Hawaiii" [15] "Idahoo" "Illinoiss" [17] "Indianaa" "Iowaa" [19] "Kansass" "Kentuckyy" [21] "Louisianaa" "Mainee" [23] "Marylandd" "Massachusettss" [25] "Michigann" "Minnesotaa" [27] "Minor Outlying Islandss" "Mississippii" [29] "Missourii" "Montanaa" [31] "Nebraskaa" "Nevadaa" [33] "New Hampshiree" "New Jerseyy" [35] "New Mexicoo" "New Yorkk" [37] "North Carolinaa" "North Dakotaa" [39] "Northern Mariana Islandss" "Ohioo" [41] "Oklahomaa" "Oregonn" [43] "Pennsylvaniaa" "Puerto Ricoo" [45] "Rhode Islandd" "South Carolinaa" [47] "South Dakotaa" "Tennesseee" [49] "Texasx" "U.S. Virgin Islandss" [51] "Utahh" "Vermontt" [53] "Virginiaa" "Washingtonn" [55] "West Virginiaa" "Wisconsinn" [57] "Wyomingg"
Example
gsub('.{1}$','',x2)
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("AKK", "ALL", "ARR", "ASS", "AZZ", "CAA", "COO", "CTT", "DCC", "DEE", "FFLL", "GAA", "GUU", "HII", "IAA", "IDD", "ILL", "INN", "KSS", "KYY", "LAA", "MAA", "MDD", "MEE", "MII", "MNN", "MOO", "MPP", "MSS", "MTT", "NCC", "NDD", "NEE", "NHH", "NJJ", "NMM", "NVV", "NYY", "OHH", "OKK", "ORR", "PAA", "PRR", "RII", "SCC", "SDD", "TNN", "TXX", "UMM", "UTT", "VAA", "VII", "VTT", "WAA", "WII", "WVV", "WYY") x3 [1] "AKK" "ALL" "ARR" "ASS" "AZZ" "CAA" "COO" "CTT" "DCC" "DEE" [11] "FFLL" "GAA" "GUU" "HII" "IAA" "IDD" "ILL" "INN" "KSS" "KYY" [21] "LAA" "MAA" "MDD" "MEE" "MII" "MNN" "MOO" "MPP" "MSS" "MTT" [31] "NCC" "NDD" "NEE" "NHH" "NJJ" "NMM" "NVV" "NYY" "OHH" "OKK" [41] "ORR" "PAA" "PRR" "RII" "SCC" "SDD" "TNN" "TXX" "UMM" "UTT" [51] "VAA" "VII" "VTT" "WAA" "WII" "WVV" "WYY" gsub('.{1}$','',x3) [1] "AK" "AL" "AR" "AS" "AZ" "CA" "CO" "CT" "DC" "DE" "FFL" "GA" [13] "GU" "HI" "IA" "ID" "IL" "IN" "KS" "KY" "LA" "MA" "MD" "ME" [25] "MI" "MN" "MO" "MP" "MS" "MT" "NC" "ND" "NE" "NH" "NJ" "NM" [37] "NV" "NY" "OH" "OK" "OR" "PA" "PR" "RI" "SC" "SD" "TN" "TX" [49] "UM" "UT" "VA" "VI" "VT" "WA" "WI" "WV" "WY"
Advertisements