如何在 R 中把字符串中的“and”替换成“&”?


我们知道单词“and”可以写成“&”。如果我们有包含用单词“and”分隔的字符串值向量的,那么我们可以用“&”来替换它。要做到这一点,我们可以使用 stringi 的 stri_replace_last 函数包。例如,如果我们有一个只包含一个元素的字符串向量,定义为 x<−“tutorialspoint and e−learning”,那么我们可以用“&”替换“and”,stri_replace_last(x,fixed='and','&')。

加载 stringi 包

library(stringi)

範例 1

 实时演示

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

範例 2

 实时演示

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

範例 3

 实时演示

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"

範例 4

 实时演示

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"

更新于:2020 年 11 月 6 日

129 次浏览

开启您的 职业生涯

完成课程即可获得认证

开始
广告