如何在 R 中分隔用特殊字符加入的字符串?


当我们处理文本数据时,很难对其进行清理,而且这种类型数据最基本的困难之一,就是值是用一些唯一字符(如特殊字符)分隔的。为此,我们可以使用 strsplit 函数,它使得分隔文本值变得容易。查看以下示例,了解如何做到这一点。

示例

 现场演示

x1<-"A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z"
x1

输出

[1] "A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z"

示例

strsplit(x1,"[-]")

输出

[[1]] [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"

示例

 现场演示

x2<-"AK:AL:AR:AS:AZ:CA:CO:CT:DC:DE:FL:GA:GU:HI:IA:ID:IL:IN:KS:KY:LA:MA:MD:ME:MI:MN:MO:MP:MS:MT:NC:ND:NE:NH:NJ:NM:NV:NY:OH:OK:OR:PA:PR:RI:SC:SD:TN:TX:UM:UT:VA:VI:VT:WA:WI:WV:WY"
x2

输出

[1] "AK:AL:AR:AS:AZ:CA:CO:CT:DC:DE:FL:GA:GU:HI:IA:ID:IL:IN:KS:KY:LA:MA:M
D:ME:MI:MN:MO:MP:MS:MT:NC:ND:NE:NH:NJ:NM:NV:NY:OH:OK:OR:PA:PR:RI:SC:SD:TN:TX:UM:UT:VA:VI:VT:WA:WI:WV:WY"

示例

strsplit(x2,"[:]")

输出

[[1]] [1] "AK" "AL" "AR" "AS" "AZ" "CA" "CO" "CT" "DC" "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"

示例

 现场演示

x3<-"AK/AL/AR/AS/AZ/CA/CO/CT/DC/DE/FL/GA/GU/HI/IA/ID/IL/IN/KS/KY/LA/MA/MD/ME/MI/MN/MO/MP/MS/MT/NC/ND/NE/NH/NJ/NM/NV/NY/OH/OK/OR/PA/PR/RI/SC/SD/TN/TX/UM/UT/VA/VI/VT/WA/WI/WV/WY"
x3

输出

[1] "AK/AL/AR/AS/AZ/CA/CO/CT/DC/DE/FL/GA/GU/HI/IA/ID/IL/IN/KS/KY/LA/MA/MD/ME/MI/MN/MO/MP/MS/MT/NC/ND/NE/NH/NJ/NM/NV/NY/OH/OK/OR/PA/PR/RI/SC/SD/TN/TX/UM/UT/VA/VI/VT/WA/WI/WV/WY"

示例

strsplit(x3,"[/]")

输出

[[1]] [1] "AK" "AL" "AR" "AS" "AZ" "CA" "CO" "CT" "DC" "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"

示例

 现场演示

x4<-"AK~AL~AR~AS~AZ~CA~CO~CT~DC~DE~FL~GA~GU~HI~IA~ID~IL~IN~KS~KY~LA~MA~MD~ME~MI~MN~MO~MP~MS~MT~NC~ND~NE~NH~NJ~NM~NV~NY~OH~OK~OR~PA~PR~RI~SC~SD~TN~TX~UM~UT~VA~VI~VT~WA~WI~WV~WY"
x4

输出

[1] "AK~AL~AR~AS~AZ~CA~CO~CT~DC~DE~FL~GA~GU~HI~IA~ID~IL~IN~KS~KY~LA~MA~MD~ME~MI~MN~MO~MP~MS~MT~NC~ND~NE~NH~NJ~NM~NV~NY~OH~OK~OR~PA~PR~RI~SC~SD~TN~TX~UM~UT~VA~VI~VT~WA~WI~WV~WY"

示例

strsplit(x4,"[~]")

输出

[[1]] [1] "AK" "AL" "AR" "AS" "AZ" "CA" "CO" "CT" "DC" "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"

示例

 现场演示

x5<-"AK*AL*AR*AS*AZ*CA*CO*CT*DC*DE*FL*GA*GU*HI*IA*ID*IL*IN*KS*KY*LA*MA*MD*ME*MI*MN*MO*MP*MS*MT*NC*ND*NE*NH*NJ*NM*NV*NY*OH*OK*OR*PA*PR*RI*SC*SD*TN*TX*UM*UT*VA*VI*VT*WA*WI*WV*WY"
x5

输出

[1] "AK*AL*AR*AS*AZ*CA*CO*CT*DC*DE*FL*GA*GU*HI*IA*ID*IL*IN*KS*KY*LA*MA*MD*ME*MI*MN*MO*MP*MS*MT*NC*ND*NE*NH*NJ*NM*NV*NY*OH*OK*OR*PA*PR*RI*SC*SD*TN*TX*UM*UT*VA*VI*VT*WA*WI*WV*WY"

示例

strsplit(x5,"[*]")

输出

[[1]] [1] "AK" "AL" "AR" "AS" "AZ" "CA" "CO" "CT" "DC" "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"

示例

 现场演示

x6<-c("AK*AL*AR*AS*AZ*CA","CO*CT*DC*DE*FL*GA","GU*HI*IA*ID*IL*IN*KS","KY*LA*MA*MD*ME*MI","MN*MO*MP*MS*MT*NC","ND*NE*NH*NJ*NM*NV","NY*OH*OK*OR*PA*PR","RI*SC*SD*TN*TX*UM","UT*VA*VI*VT","WA*WI*WV*WY")
x6

输出

[1] "AK*AL*AR*AS*AZ*CA" "CO*CT*DC*DE*FL*GA" "GU*HI*IA*ID*IL*IN*KS"
[4] "KY*LA*MA*MD*ME*MI" "MN*MO*MP*MS*MT*NC" "ND*NE*NH*NJ*NM*NV"
[7] "NY*OH*OK*OR*PA*PR" "RI*SC*SD*TN*TX*UM" "UT*VA*VI*VT"
[10] "WA*WI*WV*WY"

示例

strsplit(x6,"[*]")

输出

[[1]] [1] "AK" "AL" "AR" "AS" "AZ" "CA"
[[2]] [1] "CO" "CT" "DC" "DE" "FL" "GA"
[[3]] [1] "GU" "HI" "IA" "ID" "IL" "IN" "KS"
[[4]] [1] "KY" "LA" "MA" "MD" "ME" "MI"
[[5]] [1] "MN" "MO" "MP" "MS" "MT" "NC"
[[6]] [1] "ND" "NE" "NH" "NJ" "NM" "NV"
[[7]] [1] "NY" "OH" "OK" "OR" "PA" "PR"
[[8]] [1] "RI" "SC" "SD" "TN" "TX" "UM"
[[9]] [1] "UT" "VA" "VI" "VT"
[[10]] [1] "WA" "WI" "WV" "WY"

更新于:16-Oct-2020

130 次浏览

开启你的 职业生涯

完成课程获得认证

开始学习
广告