整理自统计之都论坛
方法一 使用strsplit函数
a <- "aggcacggaaaaacgggaataacggaggaggacttggcacggcattacacggagg" b <- strsplit(as.character(a),"ag") length(b[[1]]) - 1 ##子字符串"ag"的出现个数
a <- "aggcacggaaaaacgggaataacggaggaggacttggcacggcattacacggagg" b <- strsplit(as.character(a),"ag") regexpr("ag",a) gregexpr("ag",a) gregexpr("a.g",a) attr(gregexpr("a.g",a)[[1]], "match.length") #提取子模式长度
library(stringr) str_count("1212345", c("12", "23", "00"))
一个使用实例
计算a*g中间有0-5个任意字母的频率
library(stringr) str <- "aggcacggaaaaacgggaataacggaggaggacttggcacggcattacacggagg" n <- 0:5 patterns <- sapply(n,function(i) { paste0("a\\w{",i,"}g") }) counts <- str_count(str,patterns) names(counts) <- n counts
使用R完成字符串的子字符串频率统计,码迷,mamicode.com
原文地址:http://blog.csdn.net/yucan1001/article/details/24742871