
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Different Elements Between Two String Vectors in R
Just like numerical vectors, we can find the different elements between two string vectors if there exists any. For this purpose, we can use setdiff function. For example, if we have a vector V1 that contains a, b, c, d, e, f and the other vector V2 that contains a, e, h, k, l, p, r, u, v, w then the different elements between these two vectors can be found as setdiff(V1,V2).
Example
x1<-LETTERS[1:26] x1
Output
[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"
Example
x2<-LETTERS[5:21] x2
Output
[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"
Example
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
Output
[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"
Example
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
Output
[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"
Example
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
Output
[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"
Example
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
Output
[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"
Advertisements