# 如何计算一列string每行中给定字符的出现次数？

``q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"))` `

` `string.counter<-function(strings, pattern){ counts<-NULL for(i in 1:length(strings)){ counts[i]<-length(attr(gregexpr(pattern,strings[i])[[1]], "match.length")[attr(gregexpr(pattern,strings[i])[[1]], "match.length")>0]) } return(counts) } string.counter(strings=q.data\$string, pattern="a") number string number.of.a 1 1 greatgreat 2 2 2 magic 1 3 3 not 0` `

### 7 Solutions collect form web for “如何计算一列string每行中给定字符的出现次数？”

stringr包提供了`str_count`函数，这似乎是你所感兴趣的

` `# Load your example data q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = F) library(stringr) # Count the number of 'a's in each element of string q.data\$number.of.a <- str_count(q.data\$string, "a") q.data # number string number.of.a #1 1 greatgreat 2 #2 2 magic 1 #3 3 not 0` `

` `x <- q.data\$string sapply(regmatches(x, gregexpr("g", x)), length) # [1] 2 1 0` `

` `lengths(regmatches(x, gregexpr("g", x)))` `
` `nchar(as.character(q.data\$string)) -nchar( gsub("a", "", q.data\$string)) [1] 2 1 0` `

` ` q.data<-q.data[rep(1:NROW(q.data), 1000),] str(q.data) 'data.frame': 3000 obs. of 3 variables: \$ number : int 1 2 3 1 2 3 1 2 3 1 ... \$ string : Factor w/ 3 levels "greatgreat","magic",..: 1 2 3 1 2 3 1 2 3 1 ... \$ number.of.a: int 2 1 0 2 1 0 2 1 0 2 ... benchmark( Dason = { q.data\$number.of.a <- str_count(as.character(q.data\$string), "a") }, Tim = {resT <- sapply(as.character(q.data\$string), function(x, letter = "a"){ sum(unlist(strsplit(x, split = "")) == letter) }) }, DWin = {resW <- nchar(as.character(q.data\$string)) -nchar( gsub("a", "", q.data\$string))}, Josh = {x <- sapply(regmatches(q.data\$string, gregexpr("g",q.data\$string )), length)}, replications=100) #----------------------- test replications elapsed relative user.self sys.self user.child sys.child 1 Dason 100 4.173 9.959427 2.985 1.204 0 0 3 DWin 100 0.419 1.000000 0.417 0.003 0 0 4 Josh 100 18.635 44.474940 17.883 0.827 0 0 2 Tim 100 3.705 8.842482 3.646 0.072 0 0` `
` `sum(charToRaw("abc.d.aa") == charToRaw('.'))` `

` `sapply(as.character(q.data\$string), function(x, letter = "a"){ sum(unlist(strsplit(x, split = "")) == letter) }) greatgreat magic not 2 1 0` `

` `countLetter <- function(charvec, letter){ sapply(charvec, function(x, letter){ sum(unlist(strsplit(x, split = "")) == letter) }, letter = letter) } countLetter(as.character(q.data\$string),"a")` `
` `s <- "aababacababaaathhhhhslsls jsjsjjsaa ghhaalll" p <- "a" s2 <- gsub(p,"",s) numOcc <- nchar(s) - nchar(s2)` `

` `HowManySpaces<-nchar(DF\$string)-nchar(gsub(" ","",DF\$string)) # count spaces in DF\$string` `
• 正则expression式不能以给定的后缀结尾
• C ++：我应该使用哪个正则expression式库？
• 正则expression式从C＃中去除行注释
• 正则expression式 - 匹配整个string
• XPath - node（）和text（）之间的区别
• R：用gsubreplace字符，如何创build一个函数？
• Visual Studio查找和replace正则expression式的帮助
• 在单个正则expression式中折叠并捕获重复模式
• 正则expression式来提取子string，由于某种原因返回2个结果
• 用Javascript正则expression式匹配重音字符
• jQuery的JavaScript正则expression式replace\ n