计算R中字符串中某个字符出现的次数
在本文中,我们将讨论如何在 R 编程语言中统计 String 中某个字符出现的次数。
方法一:使用stringR包
R 中的 stringR 包用于执行字符串操作。它需要明确安装在工作空间中才能访问其方法和例程。
install.packages("stringr")
stringr 包提供了一个str_count()方法,用于计算指定为函数参数的特定模式的出现次数。模式可以是单个字符或一组字符。与表达式匹配的任何实例都会导致计数增加。此方法也可以在字符串向量上调用,并返回一个单独的计数向量,其中包含找到的模式匹配数的单独计数。但是,这种方法仅被认为是正则表达式匹配的近似。如果找不到匹配项,则返回 0。
Syntax: str_count(str, pattern = “”)
Parameters :
- str – The string to count the occurrences of
- pattern – the pattern to match to
示例 1:
R
library(stringr)
# declaring string
str1 = "$geeks%for!geeks%"
# declaring character to find
ch1 = "a"
print("Count for character a")
str_count(str1,ch1)
ch2 = "%"
print("Count for character %")
str_count(str1,ch2)
R
library(stringr)
# declaring string
str = c("$geeks%for!geeks%","cs^e%portal",
"le%..e3oten","joinnow3")
print ("Original vector")
print (str)
# declaring character to find
ch2 = "%"
print("Count for character %")
str_count(str,ch2)
R
# declaring string
str1 = "$geeks%for!geeks%"
# declaring character to find
ch1 = "a"
print("Count for character a")
lengths(regmatches(str1, gregexpr(ch1, str1)))
ch2 = "%"
print("Count for character %")
lengths(regmatches(str1, gregexpr(ch2, str1)))
R
# declaring string
str = c("$geeks%for!geeks%","cs^e%portal","le%..e3oten","joinnow3")
print ("Original vector")
print (str)
# declaring character to find
ch2 = "%"
print("Count for character %")
lengths(regmatches(str, gregexpr(ch2, str)))
输出
[1] "Count for character a"
[1] 0
[1] "Count for character %"
[1] 2
示例 2:
电阻
library(stringr)
# declaring string
str = c("$geeks%for!geeks%","cs^e%portal",
"le%..e3oten","joinnow3")
print ("Original vector")
print (str)
# declaring character to find
ch2 = "%"
print("Count for character %")
str_count(str,ch2)
输出
[1] "Original vector"
[1] "$geeks%for!geeks%" "cs^e%portal" "le%..e3oten" "joinnow3"
[1] "Count for character %"
[1] 2 1 1 0
方法 2:使用 regmatches() 方法
此方法使用基 R 中可用的各种方法来计算特定字符在 R 中的出现次数。 gregexpr() 方法用于返回与函数参数列表的特定模式匹配的子列表列表。在这种情况下,使用的模式匹配区分大小写。
句法:
gregexpr(pattern, text)
接下来是在 R 中应用 regmatches() 方法,该方法用于提取并替换由 gregexpr() 方法返回的匹配子字符串列表。第一个参数是原始向量,第二个参数是作为前一个方法的结果返回的对象。然后应用 lengths() 方法以返回参数向量的所有元素的单独长度。
句法:
lengths(x)
示例 1:
电阻
# declaring string
str1 = "$geeks%for!geeks%"
# declaring character to find
ch1 = "a"
print("Count for character a")
lengths(regmatches(str1, gregexpr(ch1, str1)))
ch2 = "%"
print("Count for character %")
lengths(regmatches(str1, gregexpr(ch2, str1)))
输出
[1] "Count for character a"
[1] 0
[1] "Count for character %"
[1] 2
示例 2:
电阻
# declaring string
str = c("$geeks%for!geeks%","cs^e%portal","le%..e3oten","joinnow3")
print ("Original vector")
print (str)
# declaring character to find
ch2 = "%"
print("Count for character %")
lengths(regmatches(str, gregexpr(ch2, str)))
输出
[1] "Original vector"
[1] "$geeks%for!geeks%" "cs^e%portal" "le%..e3oten" "joinnow3"
[1] "Count for character %"
[1] 2 1 1 0