How to replace “and” in a string with “&” in R?

R ProgrammingServer Side ProgrammingProgramming

We know that the word “and” can be written as “&”. If we have vectors that contain string values separated with word “and” then we can replace it with “&”. To do this, we can use stri_replace_last function of stringi package. For example, if we have a string vector that contain only one element defined as x<−“tutorialspoint and e−learning” then we can replace “and” with “&” stri_replace_last(x,fixed='and','&').

Loading stringi package

library(stringi)

Example1

 Live Demo

x1<−"India and China"
x1
[1] "India and China"
stri_replace_last(x1,fixed='and','&')
[1] "India & China"

Example2

 Live Demo

x2<−"Low, medium, and high"
x2
[1] "Low, medium, and high"
stri_replace_last(x2,fixed='and','&')
[1] "Low, medium, & high"

Example3

 Live Demo

x3<−c("AK and AL", "AR and AS", "AZ and CA", "CO and CT", "DC and DE", "FL and GA", "GU and HI", "IA and ID", "IL and IN", "KS and KY", "LA and MA", "MD and ME", "MI and MN", "MO and MP", "MS and MT", "NC and ND", "NE and NH", "NJ and NM", "NV and NY", "OH and OK", "OR and PA", "PR and RI", "SC and SD", "TN and TX", "UM and UT", "VA and VI", "VT and WA", "WI and WV", "and WY")
x3
[1] "AK and AL" "AR and AS" "AZ and CA" "CO and CT" "DC and DE" "FL and GA"
[7] "GU and HI" "IA and ID" "IL and IN" "KS and KY" "LA and MA" "MD and ME"
[13] "MI and MN" "MO and MP" "MS and MT" "NC and ND" "NE and NH" "NJ and NM"
[19] "NV and NY" "OH and OK" "OR and PA" "PR and RI" "SC and SD" "TN and TX"
[25] "UM and UT" "VA and VI" "VT and WA" "WI and WV" "and WY"
stri_replace_last(x3,fixed='and','&')
[1] "AK & AL" "AR & AS" "AZ & CA" "CO & CT" "DC & DE" "FL & GA" "GU & HI"
[8] "IA & ID" "IL & IN" "KS & KY" "LA & MA" "MD & ME" "MI & MN" "MO & MP"
[15] "MS & MT" "NC & ND" "NE & NH" "NJ & NM" "NV & NY" "OH & OK" "OR & PA"
[22] "PR & RI" "SC & SD" "TN & TX" "UM & UT" "VA & VI" "VT & WA" "WI & WV"
[29] "& WY"

Example4

 Live Demo

x4<−c("Alabama and Alaska", "American Samoa and Arizona", "Arkansas and California", "Colorado and Connecticut", "Delaware and District of Columbia", "Florida and Georgia", "Guam and Hawaii", "Idaho and Illinois", "Indiana and Iowa", "Kansas and Kentucky", "Louisiana and Maine", "Maryland and Massachusetts", "Michigan and Minnesota", "Minor Outlying Islands and Mississippi", "Missouri and Montana", "Nebraska and Nevada", "New Hampshire and New Jersey", "New Mexico and New York", "North Carolina and North Dakota", "Northern Mariana Islands and Ohio","Oklahoma and Oregon", "Pennsylvania and Puerto Rico", "Rhode Island and South Carolina", "South Dakota and Tennessee", "Texas and U.S. Virgin Islands", "Utah and Vermont", "Virginia and Washington", "West Virginia and Wisconsin", "and", "Wyoming")
x4
[1] "Alabama and Alaska"
[2] "American Samoa and Arizona"
[3] "Arkansas and California"
[4] "Colorado and Connecticut"
[5] "Delaware and District of Columbia"
[6] "Florida and Georgia"
[7] "Guam and Hawaii"
[8] "Idaho and Illinois"
[9] "Indiana and Iowa"
[10] "Kansas and Kentucky"
[11] "Louisiana and Maine"
[12] "Maryland and Massachusetts"
[13] "Michigan and Minnesota"
[14] "Minor Outlying Islands and Mississippi"
[15] "Missouri and Montana"
[16] "Nebraska and Nevada"
[17] "New Hampshire and New Jersey"
[18] "New Mexico and New York"
[19] "North Carolina and North Dakota"
[20] "Northern Mariana Islands and Ohio"
[21] "Oklahoma and Oregon"
[22] "Pennsylvania and Puerto Rico"
[23] "Rhode Island and South Carolina"
[24] "South Dakota and Tennessee"
[25] "Texas and U.S. Virgin Islands"
[26] "Utah and Vermont"
[27] "Virginia and Washington"
[28] "West Virginia and Wisconsin"
[29] "and"
[30] "Wyoming"
stri_replace_last(x4,fixed='and','&')
[1] "Alabama & Alaska"
[2] "American Samoa & Arizona"
[3] "Arkansas & California"
[4] "Colorado & Connecticut"
[5] "Delaware & District of Columbia"
[6] "Florida & Georgia"
[7] "Guam & Hawaii"
[8] "Idaho & Illinois"
[9] "Indiana & Iowa"
[10] "Kansas & Kentucky"
[11] "Louisiana & Maine"
[12] "Maryland & Massachusetts"
[13] "Michigan & Minnesota"
[14] "Minor Outlying Islands & Mississippi"
[15] "Missouri & Montana"
[16] "Nebraska & Nevada"
[17] "New Hampshire & New Jersey"
[18] "New Mexico & New York"
[19] "North Carolina & North Dakota"
[20] "Northern Mariana Islands & Ohio"
[21] "Oklahoma & Oregon"
[22] "Pennsylvania & Puerto Rico"
[23] "Rhode Island & South Carolina"
[24] "South Dakota & Tennessee"
[25] "Texas and U.S. Virgin Isl&s"
[26] "Utah & Vermont"
[27] "Virginia & Washington"
[28] "West Virginia & Wisconsin"
[29] "&"
[30] "Wyoming"
raja
Published on 06-Nov-2020 13:06:51
Advertisements