Should I be trying to imitate the control flow in this example R code?
In my opinion, this function is a nice example of R code. Your interpretation of the code is correct. One unnecessary command is y == 0 since the information is redundant given that x == "H" is already known. In this case, y == 0 is identical to c(TRUE, !(x == "H"), TRUE).

The code can be simplified. The object wz can be created more easily:

calc_streak &lt;- function(x){
  wz &lt;- c(1, which(x == "M") + 1, length(x) + 2)
  streak &lt;- diff(wz) - 1
  return(streak)
}


Alternatively and probabily even more efficiently, you can create a logical index indicating the positions of the "M"s in the vector and calculate streak more directly:

calc_streak &lt;- function(x){
  idx &lt;- x == "M"
  streak &lt;- c(!idx[1], diff(which(idx)) - 1, !tail(idx, 1))
  return(streak)
}


The ! sign indicates logical not. If numeric and logical values are combined in one vector, the logical values are cast into numeric values. TRUE becomes 1, FALSE becomes 0.