Hi, You could also use these: library(Biostrings) gregexpr2("AB",new) #[[1]] #[1] 4 10 14
length(unlist(gregexpr2("AB",new))) #[1] 3 length(unlist(gregexpr2("BA",new))) #[1] 2 matchPattern("AB",new) # Views on a 16-letter BString subject #subject: AAAABBBBBABBBABB #views: # start end width #[1] 4 5 2 [AB] #[2] 10 11 2 [AB] #[3] 14 15 2 [AB] res1<-matchPattern("AB",new) length(res1) #[1] 3 res2<-matchPattern("BA",new) length(res2) #[1] 2 #another string new1<-c("ABABAAABABAAAABABABBA") res2<-matchPattern("BA",new1) length(res2) #[1] 7 length(unlist(gregexpr2("BA",new1))) #[1] 7 A.K. ----- Original Message ----- From: rnewbie565 <amy.jacobs...@gmail.com> To: r-help@r-project.org Cc: Sent: Thursday, August 2, 2012 5:27 PM Subject: [R] text search in r I am trying to count the number of times that the characters in a string change For example- new=c(AAAABBBBBABBBABB) I want to find the number of times that B changes to A and the number of times that A changes to B. I tried the grep command but I only figured out the positions of when B changes to A when I only need the number of times it occurs. -- View this message in context: http://r.789695.n4.nabble.com/text-search-in-r-tp4638961.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.