如何在 R 中找到两个字符串向量之间的不同元素?


就像数值向量一样,如果存在,我们可以在两个字符串向量之间找到不同的元素。为此,我们可以使用 setdiff 函数。例如,如果我们有一个包含 a、b、c、d、e、f 的向量 V1 和另一个包含 a、e、h、k、l、p、r、u、v、w 的向量 V2,那么我们可以通过 setdiff(V1,V2) 找到这两个向量之间的不同元素。

示例

 在线演示

x1<-LETTERS[1:26]
x1

输出

[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<-LETTERS[5:21]
x2

输出

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

示例

 在线演示

x3<-c("Alabama", "Alaska", "American Samoa", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Minor Outlying Islands", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Northern Mariana Islands", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Puerto Rico", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "U.S. Virgin Islands", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming")
x3

输出

[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"

示例

 在线演示

x4<-c("Alabama", "Alaska", "American Samoa", "Arizona", "Arkansas", "California","Florida", "Georgia", "Guam", "Hawaii", "Idaho", "Louisiana", "Maine", "Maryland", "Mississippi", "Missouri", "New Jersey", "New Mexico", "New York", "North Carolina", "Puerto Rico", "Rhode Island", "South Carolina", "South Dakota", "Tennessee")
x4

输出

[1] "Alabama" "Alaska" "American Samoa" "Arizona"
[5] "Arkansas" "California" "Florida" "Georgia"
[9] "Guam" "Hawaii" "Idaho" "Louisiana"
[13] "Maine" "Maryland" "Mississippi" "Missouri"
[17] "New Jersey" "New Mexico" "New York" "North Carolina"
[21] "Puerto Rico" "Rhode Island" "South Carolina" "South Dakota"
[25] "Tennessee"
setdiff(x3,x4)
[1] "Colorado" "Connecticut"
[3] "Delaware" "District of Columbia"
[5] "Illinois" "Indiana"
[7] "Iowa" "Kansas"
[9] "Kentucky" "Massachusetts"
[11] "Michigan" "Minnesota"
[13] "Minor Outlying Islands" "Montana"
[15] "Nebraska" "Nevada"
[17] "New Hampshire" "North Dakota"
[19] "Northern Mariana Islands" "Ohio"
[21] "Oklahoma" "Oregon"
[23] "Pennsylvania" "Texas"
[25] "U.S. Virgin Islands" "Utah"
[27] "Vermont" "Virginia"
[29] "Washington" "West Virginia"
[31] "Wisconsin" "Wyoming"

示例

 在线演示

x5<-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")
x5

输出

[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", "CA", "CO", "CT", "HI", "IA", "ID", "IL", "MD", "ME", "MI", "MN", "MO", "MP", "MS", "MT", "NC", "ND", "NE", "NH", "OR", "PA", "PR", "RI", "SC")
x6

输出

[1] "AK" "CA" "CO" "CT" "HI" "IA" "ID" "IL" "MD" "ME" "MI" "MN" "MO" "MP" "MS"
[16] "MT" "NC" "ND" "NE" "NH" "OR" "PA" "PR" "RI" "SC"

更新时间:08-Oct-2020

1K+ 次浏览

开启你的 职业生涯

完成课程,获得认证

开始
广告