title: Create dummy variable with loop or dplyr
body: <p>I am working on a panel dataset in which there are firms and each firm reports its financial earnings. I created a dummy var. which reports 1 if there is a loss for a certain year for each firm. I am trying to count the number of two losses a year after year and create a dummy var. &quot;dummy2loss&quot; that gets 1 if there are two-year losses. The dataset looks like that
  <a href="https://i.stack.imgur.com/ZgjKu.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>and the results are supposed to be</p>
  <p><a href="https://i.stack.imgur.com/11kZv.png" rel="nofollow noreferrer">enter image description here</a></p>