title: Coding a variable based on time trends in R?
body: <p>So my question isn't about R code <em>per se</em>, it is more of a code structure or logic problem.  Say I have travel data from every night in 2017 for a group of people.  Every line in my data frame is the location where a person was during the night (by county).  So it looks something like this:</p>
  <pre><code>x &lt;- data.frame(&quot;name&quot; = c(&quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;Dora&quot;, &quot;Dora&quot;, &quot;Dora&quot;, &quot;Dora&quot;), 
  &quot;date&quot; = c(&quot;1/1/19&quot;,&quot;1/2/19&quot;,&quot;1/3/19&quot;,&quot;1/4/19&quot;,&quot;1/1/19&quot;,&quot;1/2/19&quot;,&quot;1/3/19&quot;,&quot;1/4/19&quot;),
  &quot;county&quot; = c(&quot;county 1&quot;, &quot;county 1&quot;, &quot;county 2&quot;, &quot;county 1&quot;, &quot;county 1&quot;, &quot;county 1&quot;, &quot;county 2&quot;, &quot;county 2&quot;))
  </code></pre>
  <p>In reality the data is much more complex than this.  But this is the basic idea.  In this scenario John leaves his home county (county 1) to spend a night visiting county 2.  In contrast Dora leaves her home county 1 and moves to county 2 (she does not return to county 1 within the timeframe of the data).  How would I go about coding a variable that would tell me who is a traveler (e.g. John) and who is a mover (e.g. Dora)?  I want to label John at 'home' for all nights but his one county 2 night when I want him labeled 'traveler'.  I want Dora labeled 'home' for every county 1 night and 'moved' for every county 2 night.</p>
  <p>Assume someone could travel for as many as 363 nights in a row to more than one county.</p>
  <p>I'm not sure my data is in the right format to answer this question, but it is very large (too large to make wide).</p>