Should I be trying to imitate the control flow in this example R code? I have heard that some patterns in R code are really anti-patterns and thus I am trying to imitate superior examples of R code.

I am confused about control flow.

I have been reviewing some R code from openintro.org;
the original exercise is explained at:

http://www.openintro.org/download.php?file=os2_lab_02A&amp;referrer=/stat/labs.php

One of the very early examples deals with counting "hit" or "miss" streaks within a vector of "H" and "M" characters.  We consider a winning streak to be at least one H, and each streak ends in M.  So H M M H H M counts as a streak of 1, a streak of 0, a streak of 2, etc.

So, for example, we start with a sequence like kobe$basket:

  [1] "H" "M" "M" "H" "H" "M" "M" "M" "M" "H" "H" "H" "M" "H" "H" "M" "M" "H"
 [19] "H" "H" "M" "M" "H" "M" "H" "H" "H" "M" "M" "M" "M" "M" "M" "H" "M" "H"
 [37] "M" "M" "H" "H" "H" "H" "M" "H" "M" "M" "H" "M" "M" "H" "M" "M" "H" "M"
 [55] "H" "H" "M" "M" "H" "M" "H" "H" "M" "H" "M" "M" "M" "H" "M" "M" "M" "M"
 [73] "H" "M" "H" "M" "M" "H" "M" "M" "H" "H" "M" "M" "M" "M" "H" "H" "H" "M"
 [91] "M" "H" "M" "M" "H" "M" "H" "H" "M" "H" "M" "M" "H" "M" "M" "M" "H" "M"
[109] "H" "H" "H" "M" "H" "H" "H" "M" "H" "M" "H" "M" "M" "M" "M" "M" "M" "H"
[127] "M" "H" "M" "M" "M" "M" "H"


and if we call calc_streak on that, we get:

 [1] 1 0 2 0 0 0 3 2 0 3 0 1 3 0 0 0 0 0 1 1 0 4 1 0 1 0 1 0 1 2 0 1 2 1 0 0 1 0
[39] 0 0 1 1 0 1 0 2 0 0 0 3 0 1 0 1 2 1 0 1 0 0 1 3 3 1 1 0 0 0 0 0 1 1 0 0 0 1


The "1 0 2" of the answer indicates the streak of 1, the streak of 0, the streak of 2 mentioned above.

The following function is referred to as "calc_streak" but apparently that name doesn't show up in the code listing.

function(x){
y &lt;- rep(0,length(x))
y[x == "H"] &lt;- 1
y &lt;- c(0, y, 0)
wz &lt;- which(y == 0)
streak &lt;- diff(wz) - 1
return(streak)
}


I think the first line makes a big vector of "0" values, just as long as the input, so in this case it should be 133 entries long.

I think the second line goes through that vector and rewrites the "0" value to "1" if the input has an "H."  

The third line might be putting an extra "0" at the front and back of the vector.  I think it's in there to make the "diff" work correctly, so that the first winning streak shows up as a "1" value.

I think the object "wz" is a vector of integers. (I suspect this is a short vector, just containing the indices of the entries in x that satisfy the test.)

So first and foremost, if I am misunderstanding the way this example code works, please correct me.

But my primary question is - Should I regard the example R code as a good example of control flow and imitate it in my other programs?

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