[R] text search in r

arun smartpink111 at yahoo.com
Fri Aug 3 04:35:10 CEST 2012


Hi,

One more solution:

new<-"AAAABBBBBABBBABB"
resAB<-count(unlist(strsplit(gsub("AB",1,new),""))==1)[2,]
 resAB
#     x freq
#2 TRUE    3
resBA<-count(unlist(strsplit(gsub("BA",2,new),""))==2)[2,]
 resBA
#     x freq
#2 TRUE    2
new1<-c("ABABAAABABAAAABABABBA")
resAB<-count(unlist(strsplit(gsub("AB",1,new1),""))==1)[2,]
 resAB
 #    x freq
#2 TRUE    7
resBA<-count(unlist(strsplit(gsub("BA",2,new1),""))==2)[2,]
 resBA
#     x freq
#2 TRUE    7


A.K.


----- Original Message -----
From: rnewbie565 <amy.jacobson2 at gmail.com>
To: r-help at 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 at 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.




More information about the R-help mailing list