Using R, what would be the best way to count the number of changes in a sequence of character values from an index. For example, I have a fixed number of sites:
sites<-as.factor(LETTERS[seq(from=1,to=20)])
From those sites, some are protected, while others are open to fishing,
protected<-as.factor(c("A","D","E","M","L","S"))
Using a simulation, I got this sequence of sites (a combination of protected/non protected sites)
result<-as.factor(c("A","A","A","B","C","D","D","L","L","F","F","T","S","N"))
Basically, I want to count how many times in my result sequence, there is a change from “protected” to “non-protected” sites. In this example, the answer that I am looking would be 3, since “A”, which is a protected site is moving to “B” that is non-protected (one move), “B” is moving to “C” (both unprotected so is not changing),…, “L” to “N” (two moves), etc.
Find where you change from Protected to Non-protected with %in% and diff. Then count up the values you want. Here, Protected -> non-protected gives -1.