How to extract string before slash from a vector in R?

R ProgrammingServer Side ProgrammingProgramming

If a vector contains string values and they are separated with a special character (This special character can be anything, also it is not necessarily to be a special character) and we want to extract only the values that exists before that special then we can use gsub function. For example, if we have a vector x that contain "UT/YT", "IST/IT", "PST/PT" then gsub can help us to extract UT, IST, PST.

Example

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

Output

x1
[1] "Alabama/Alaska"
[2] "American Samoa/Arizona"
[3] "Arkansas/California"
[4] "Colorado/Connecticut"
[5] "Delaware/District of Columbia/Florida"
[6] "Georgia/Guam"
[7] "Hawaii/Idaho"
[8] "Illinois/Indiana"
[9] "Iowa/Kansas"
[10] "Kentucky/Louisiana"
[11] "Maine/Maryland"
[12] "Massachusetts/Michigan"
[13] "Minnesota/Minor Outlying Islands"
[14] "Mississippi/Missouri"
[15] "Montana/Nebraska"
[16] "Nevada/New Hampshire"
[17] "New Jersey/New Mexico"
[18] "New York/North Carolina"
[19] "North Dakota/Northern Mariana Islands"
[20] "Ohio/Oklahoma"
[21] "Oregon/Pennsylvania"
[22] "Puerto Rico/Rhode Island"
[23] "South Carolina/South Dakota"
[24] "Tennessee/Texas"
[25] "U.S. Virgin Islands/Utah"
[26] "Vermont/Virginia"
[27] "Washington/West Virginia"
[28] "Wisconsin/Wyoming"

Example

gsub("/.*$","",x1)

Output

[1] "Alabama" "American Samoa" "Arkansas"
[4] "Colorado" "Delaware" "Georgia"
[7] "Hawaii" "Illinois" "Iowa"
[10] "Kentucky" "Maine" "Massachusetts"
[13] "Minnesota" "Mississippi" "Montana"
[16] "Nevada" "New Jersey" "New York"
[19] "North Dakota" "Ohio" "Oregon"
[22] "Puerto Rico" "South Carolina" "Tennessee"
[25] "U.S. Virgin Islands" "Vermont" "Washington"
[28] "Wisconsin"

Example

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

Output

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

Example

gsub("/.*$","",x2)

Output

[1] "AK" "AR" "AZ" "CO" "DC" "FL" "GU" "IA" "IL" "KS" "LA" "MD" "MI" "MO" "MS"
[16] "NC" "NE" "NJ" "NV" "OH" "OR" "PR" "SC" "TN" "UM" "VA" "VT" "WI" "WY"

Example

x3<-c("Afghanistan Albania Algeria Andorra Angola Antigua and Barbuda Argentina Armenia Australia Austria/Azerbaijan","The Bahamas Bahrain Bangladesh Barbados Belarus Belgium Belize Benin Bhutan Bolivia Bosnia and Herzegovina Botswana Brazil Brunei Bulgaria Burkina Faso/Burundi","Cabo Verde Cambodia Cameroon Canada Central African Republic Chad Chile China Colombia Comoros Costa Rica Côte d’Ivoire Croatia Cuba Cyprus/Czech Republic","Denmark Djibouti Dominica/Dominican Republic","Ecuador Egypt El Salvador Equatorial Guinea Eritrea Estonia Eswatini/Ethiopia")

Output

x3
[1] "Afghanistan Albania Algeria Andorra Angola Antigua and Barbuda Argentina Armenia Australia Austria/Azerbaijan"
[2] "The Bahamas Bahrain Bangladesh Barbados Belarus Belgium Belize Benin Bhutan Bolivia Bosnia and Herzegovina Botswana Brazil Brunei Bulgaria Burkina Faso/Burundi"
[3] "Cabo Verde Cambodia Cameroon Canada Central African Republic Chad Chile China Colombia Comoros Costa Rica Côte d’Ivoire Croatia Cuba Cyprus/Czech Republic"
[4] "Denmark Djibouti Dominica/Dominican Republic"
[5] "Ecuador Egypt El Salvador Equatorial Guinea Eritrea Estonia Eswatini/Ethiopia"

Example

gsub("/.*$","",x3)

Output

[1] "Afghanistan Albania Algeria Andorra Angola Antigua and Barbuda Argentina Armenia Australia Austria"
[2] "The Bahamas Bahrain Bangladesh Barbados Belarus Belgium Belize Benin Bhutan Bolivia Bosnia and Herzegovina Botswana Brazil Brunei Bulgaria Burkina Faso"
[3] "Cabo Verde Cambodia Cameroon Canada Central African Republic Chad Chile China Colombia Comoros Costa Rica Côte d’Ivoire Croatia Cuba Cyprus"
[4] "Denmark Djibouti Dominica"
[5] "Ecuador Egypt El Salvador Equatorial Guinea Eritrea Estonia Eswatini"
raja
Published on 17-Oct-2020 09:47:30
Advertisements