我们知道单词“和”可以写为“&”。如果我们的向量包含用单词“和”分隔的字符串值,则可以将其替换为“&”。为此,我们可以使用stringi包的stri_replace_last函数。例如,如果我们的字符串向量仅包含一个定义为x <-“ nhooo and e-learning”的元素,则可以将“ and”替换为“&” stri_replace_last(x,fixed ='and','&' )。
正在加载stringi包
library(stringi)
x1<−"India and China" x1 [1] "India and China" stri_replace_last(x1,fixed='and','&') [1] "India & China"
x2<−"Low, medium, and high" x2 [1] "Low, medium, and high" stri_replace_last(x2,fixed='and','&') [1] "Low, medium, & high"
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"
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"