如何在 R 向量中统计特殊字符?
特殊字符通常被视为字符串值,可以使用 stringr 包中的 str_count 函数对其进行计数。例如,如果我们有一个包含 $、#、%、^、&、*、@、! 或任何其他特殊字符的向量 x,则可以使用 str_count(x,"\$") 来统计向量 x 中 $ 的数量,这可以单独完成所有特殊字符。
示例1
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
示例2
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
示例3
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
示例4
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
广告