如何在 R 中的字符串向量中查找特定字符的计数?
为了查找字符串向量中特定字符的计数,我们可以使用 nchar 函数和 gsub。例如,如果我们有一个名为 x 的向量,其中包含字符串(例如 India、Russia、Indonesia),那么我们可以查找字符 i 出现的次数,然后我们可以使用命令 nchar(gsub("[^i]","",x)),输出将是 1 1 1,因为 India 和 Indonesia 中的第一个 I 将不被视为大写字母。
示例 1
x1<-sample(c("India","Russia","Croatia","Indonesia","China"),100,replace=TRUE) x1
输出
[1] "Russia" "India" "Indonesia" "Russia" "Russia" "Indonesia" [7] "Indonesia" "Russia" "Russia" "Russia" "Indonesia" "China" [13] "India" "India" "Indonesia" "Indonesia" "India" "Croatia" [19] "India" "Indonesia" "China" "India" "China" "Russia" [25] "China" "China" "China" "Indonesia" "Russia" "India" [31] "India" "Russia" "India" "Croatia" "India" "China" [37] "China" "India" "Indonesia" "Russia" "Croatia" "China" [43] "Russia" "Croatia" "Russia" "Indonesia" "Russia" "Indonesia" [49] "Russia" "Russia" "Russia" "China" "Indonesia" "Indonesia" [55] "India" "Russia" "Croatia" "India" "Indonesia" "China" [61] "Indonesia" "Indonesia" "Croatia" "Russia" "Russia" "Russia" [67] "Croatia" "Indonesia" "China" "Indonesia" "India" "Indonesia" [73] "China" "India" "Croatia" "Indonesia" "Russia" "China" [79] "India" "Russia" "Indonesia" "India" "India" "Croatia" [85] "Russia" "Croatia" "Croatia" "Croatia" "Russia" "Russia" [91] "Indonesia" "Indonesia" "Croatia" "India" "Indonesia" "Indonesia" [97] "China" "China" "China" "Indonesia"
nchar(gsub("[^R]","",x1))
[1] 1 0 0 1 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 [38] 0 0 1 0 0 1 0 1 0 1 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 [75] 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0
nchar(gsub("[^s]","",x1))
[1] 2 0 1 2 2 1 1 2 2 2 1 0 0 0 1 1 0 0 0 1 0 0 0 2 0 0 0 1 2 0 0 2 0 0 0 0 0 [38] 0 1 2 0 0 2 0 2 1 2 1 2 2 2 0 1 1 0 2 0 0 1 0 1 1 0 2 2 2 0 1 0 1 0 1 0 0 [75] 0 1 2 0 0 2 1 0 0 0 2 0 0 0 2 2 1 1 0 0 1 1 0 0 0 1
示例 2
x2<-sample(c("Asia","Oceania","Africa","Europe","America"),100,replace=TRUE) x2
输出
[1] "Africa" "America" "America" "America" "Europe" "Europe" "Europe" [8] "Asia" "Asia" "Europe" "Oceania" "Oceania" "Asia" "Europe" [15] "Africa" "Europe" "Asia" "America" "Oceania" "Oceania" "Europe" [22] "Asia" "Europe" "Africa" "Asia" "America" "Oceania" "Europe" [29] "Asia" "Africa" "America" "Asia" "Europe" "Europe" "America" [36] "Europe" "Oceania" "Oceania" "Asia" "America" "Oceania" "Africa" [43] "Europe" "America" "Europe" "Asia" "Asia" "Oceania" "Oceania" [50] "Oceania" "Europe" "Africa" "Asia" "Africa" "Asia" "Asia" [57] "Oceania" "Africa" "Europe" "Asia" "Oceania" "Asia" "Asia" [64] "Africa" "Oceania" "Europe" "Asia" "Oceania" "Africa" "Africa" [71] "Oceania" "Europe" "Europe" "America" "Oceania" "Europe" "Africa" [78] "Asia" "Europe" "Europe" "Europe" "Europe" "Oceania" "Africa" [85] "Africa" "Africa" "Europe" "Oceania" "Oceania" "Europe" "Europe" [92] "America" "Asia" "Asia" "Europe" "Oceania" "Africa" "Africa" [99] "Oceania" "Africa"
nchar(gsub("[^a]","",x2))
[1] 1 1 1 1 0 0 0 1 1 0 2 2 1 0 1 0 1 1 2 2 0 1 0 1 1 1 2 0 1 1 1 1 0 0 1 0 2 [38] 2 1 1 2 1 0 1 0 1 1 2 2 2 0 1 1 1 1 1 2 1 0 1 2 1 1 1 2 0 1 2 1 1 2 0 0 1 [75] 2 0 1 1 0 0 0 0 2 1 1 1 0 2 2 0 0 1 1 1 0 2 1 1 2 1
nchar(gsub("[^e]","",x2))
[1] 0 1 1 1 1 1 1 0 0 1 1 1 0 1 0 1 0 1 1 1 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 1 1 [38] 1 0 1 1 0 1 1 1 0 0 1 1 1 1 0 0 0 0 0 1 0 1 0 1 0 0 0 1 1 0 1 0 0 1 1 1 1 [75] 1 1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 1 1 0 0 1 0
nchar(gsub("[^A]","",x2))
[1] 1 1 1 1 0 0 0 1 1 0 0 0 1 0 1 0 1 1 0 0 0 1 0 1 1 1 0 0 1 1 1 1 0 0 1 0 0 [38] 0 1 1 0 1 0 1 0 1 1 0 0 0 0 1 1 1 1 1 0 1 0 1 0 1 1 1 0 0 1 0 1 1 0 0 0 1 [75] 0 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 0 0 1 1 0 1
示例 3
x3<-sample(c("sunny","cloudy","rain","snow","stormy","fog"),100,replace=TRUE) x3
输出
[1] "rain" "fog" "sunny" "fog" "fog" "cloudy" "stormy" "sunny" [9] "snow" "stormy" "sunny" "snow" "snow" "cloudy" "cloudy" "cloudy" [17] "cloudy" "sunny" "stormy" "rain" "cloudy" "fog" "sunny" "rain" [25] "sunny" "snow" "rain" "stormy" "sunny" "stormy" "cloudy" "sunny" [33] "cloudy" "cloudy" "fog" "fog" "sunny" "fog" "stormy" "stormy" [41] "stormy" "stormy" "fog" "fog" "snow" "stormy" "sunny" "sunny" [49] "sunny" "fog" "fog" "stormy" "rain" "rain" "cloudy" "cloudy" [57] "snow" "stormy" "fog" "rain" "fog" "fog" "sunny" "sunny" [65] "rain" "stormy" "fog" "snow" "sunny" "sunny" "snow" "stormy" [73] "cloudy" "stormy" "fog" "rain" "rain" "rain" "rain" "fog" [81] "cloudy" "stormy" "stormy" "cloudy" "sunny" "cloudy" "cloudy" "rain" [89] "cloudy" "cloudy" "sunny" "rain" "sunny" "stormy" "snow" "fog" [97] "snow" "fog" "rain" "fog"
nchar(gsub("[^a]","",x3))
[1] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 [75] 0 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0
nchar(gsub("[^o]","",x3))
[1] 0 1 0 1 1 1 1 0 1 1 0 1 1 1 1 1 1 0 1 0 1 1 0 0 0 1 0 1 0 1 1 0 1 1 1 1 0 [38] 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 0 0 1 1 1 1 1 0 1 1 0 0 0 1 1 1 0 0 1 1 1 1 [75] 1 0 0 0 0 1 1 1 1 1 0 1 1 0 1 1 0 0 0 1 1 1 1 1 0 1
nchar(gsub("[^n]","",x3))
[1] 1 0 2 0 0 0 0 2 1 0 2 1 1 0 0 0 0 2 0 1 0 0 2 1 2 1 1 0 2 0 0 2 0 0 0 0 2 [38] 0 0 0 0 0 0 0 1 0 2 2 2 0 0 0 1 1 0 0 1 0 0 1 0 0 2 2 1 0 0 1 2 2 1 0 0 0 [75] 0 1 1 1 1 0 0 0 0 0 2 0 0 1 0 0 2 1 2 0 1 0 1 0 1 0
广告