title: Counting Instances of Multiple Variables in R
body: <p>I have a large data table Divvy (over 2.4 million records) that appears as such (some columns removed):</p>
  
  <pre><code>X   trip_id     from_station_id.x   to_station_id.x 
   1  1109420     94                  69
   2  1109421     69                  216
   3  1109427     240                 245
   4  1109431     113                 94
   5  1109433     127                 332
   3  1109429     240                 245
  </code></pre>
  
  <p>I would like to find the number of trips from each station to each opposing station. So for example, </p>
  
  <pre><code>From X     To Y     Sum
  94         69       1
  240        245      2
  </code></pre>
  
  <p>etc.  and then join it back to the inital table using dplyr to make something like the below and then limit it to distinct from_station_id/to_combos, which I'll use to map routes (I have lat/long for each station):</p>
  
  <pre><code>X   trip_id     from_station_id.x   to_station_id.x   Sum 
   1  1109420     94                  69                1
   2  1109421     69                  216               1
   3  1109427     240                 245               2
   4  1109431     113                 94                1
   5  1109433     127                 332               1
   3  1109429     240                 245               1
  </code></pre>
  
  <p>I successfully used count to get some of this, such as:</p>
  
  <pre><code>count(Divvy$from_station_id.x==94 &amp; Divvy$to_station_id.x == 69)
    x    freq
  1 FALSE 2454553
  2  TRUE      81
  </code></pre>
  
  <p>But this is obviously labor intensive as there are 300 unique stations, so well over 44k poss combinations. I created a helper table thinking I could loop it.  </p>
  
  <pre><code>n &lt;- select(Divvy, from_station_id.y )
  
    from_station_id.x 
  1                94                
  2                69                
  3               240               
  4               113               
  5               113               
  6               127               
  
     count(Divvy$from_station_id.x==n[1,1] &amp; Divvy$to_station_id.x == n[2,1])
  
        x    freq
  1 FALSE 2454553
  2  TRUE      81
  </code></pre>
  
  <p>I felt like a loop such as </p>
  
  <pre><code>output &lt;- matrix(ncol=variables, nrow=iterations)
  
  
  output &lt;- matrix()
  for(i in 1:n)(output[i, count(Divvy$from_station_id.x==n[1,1] &amp; Divvy$to_station_id.x == n[2,1]))
  </code></pre>
  
  <p>should work but come to think of it that will still only return 300 rows, not 44k, so it would have to then loop back and do n[2] &amp; n[1] etc...</p>
  
  <p>I felt like there might also be a quicker dplyr solution that would let me return a count of each combo and append it directly without the extra steps/table creation, but I haven't found it.  </p>
  
  <p>I'm newer to R and I have searched around/think I'm close, but I can't quite connect that last dot of joining that result to Divvy.  Any help appreciated.</p>