On Aug 2, 2012, at 2:27 PM, rnewbie565 wrote:

I am trying to count the number of times that the characters in a string
change
For example-
new=c(AAAABBBBBABBBABB)

Presumably you meant to quote that string.

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.

>  new=c('AAAABBBBBABBBABB')
> unlist(strsplit(new,""))
 [1] "A" "A" "A" "A" "B" "B" "B" "B" "B" "A" "B" "B" "B" "A" "B" "B"
> rle(unlist(strsplit(new,"")))
Run Length Encoding
  lengths: int [1:6] 4 5 1 3 1 2
  values : chr [1:6] "A" "B" "A" "B" "A" "B"

> paste0( rle(unlist(strsplit(new,"")))$values, collapse="")
[1] "ABABAB"
> gregexpr("AB", .Last.value)
[[1]]
[1] 1 3 5
attr(,"match.length")
[1] 2 2 2
attr(,"useBytes")
[1] TRUE


So the length of that list value could be used for the AB transitions. Similarly for the BA ones

> strrle <- paste0( rle(unlist(strsplit(new,"")))$values, collapse="")
> length( gregexpr("BA", strrle)[[1]] )
[1] 2





--
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.

David Winsemius, MD
Alameda, CA, USA

______________________________________________
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.

Reply via email to