特殊字符通常被视为字符串值,并且可以在stringr软件包的str_count函数的帮助下对其进行计数。例如,如果我们有一个向量x,其中包含$,#,%,^,&,*,@,!!或任何其他特殊字符,然后我们可以使用str_count(x,“ \\ $”)来计算向量x中$的数量,这可以分别对所有特殊字符进行。
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$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中$的数量-
str_count(x1,"\\$") [1] 56
x2<−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")
在x2中计算#的数量-
str_count(x2,"\\#") [1] 19
x3<−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") x3 [1] "AK * AL" [2] "AR * AS" [3] "AZ * CA" [4] "CO * * CT" [5] "DC * * * * DE" [6] "FL * * * * * * * * * * * * * * * *GA" [7] "GU * * HI" [8] "IA * * * ID" [9] "IL" [10] "IN" [11] "KS" [12] "KY * * LA" [13] "MA * * MD" [14] "ME * * * * MI" [15] "MN * * * MO" [16] "MP * * * * MS" [17] "MT * * * * * * NC" [18] "ND * * * NE" [19] "NH * * * * * NJ" [20] "NM * NV" [21] "NY * * * * OH" [22] "OK * * * * OR" [23] "PA * * * * * * * PR" [24] "RI * * SC" [25] "SD * TN" [26] "TX * * * * UM" [27] "UT * * * * * * * * * VA" [28] "VI * * * VT" [29] "WA * * * * * * WI" [30] "WV * WY" str_count(x3,"\\*") [1] 1 1 1 2 4 16 2 3 0 0 0 2 2 4 3 4 6 3 5 1 4 4 7 2 1 [26] 4 9 3 6 1
x4<−c("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") x4 [1] "A / / / // / / // / / / / / / / // / / // / /B" [2] "C/ / D" [3] "E F G / / / / / / / / /H" [4] "I // J ///// / / / K / / / / L" [5] "M N O P Q R /// S T U V //// / W X // Y /Z" str_count(x4,"\\/") [1] 24 2 9 14 11