怎样从 R 中向量的斜杠之前提取字符串?


如果一个向量包含字符串值,并且它们用特殊字符分隔(此特殊字符可以是任何字符,也不一定是特殊字符),并且我们只想提取该特殊字符之前存在的值,那么我们可以使用 gsub 函数。例如,如果我们有一个包含“UT/YT”、“IST/IT”、“PST/PT”的向量,那么 gsub 可以帮助我们提取 UT、IST、PST。

示例

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

输出

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"

示例

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

输出

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

示例

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

输出

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"

示例

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

输出

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

示例

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

输出

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"

示例

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

输出

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

更新于: 17-Oct-2020

2K+ 浏览量

开启你的 职业

通过完成课程获得认证

开始
广告