title: R: divide latitude and longitude into grid sections
body: <p>I have latitude and longitude points:</p>
  
  <pre><code>&gt; d1 &lt;- data.frame(lat, lon)
  &gt; head(d1)
         lat       lon
  1 43.25724 -96.01955
  2 43.25724 -95.98172
  3 43.25724 -95.92336
  4 43.25616 -96.40973
  5 43.25616 -96.25733
  6 43.25616 -96.17735
  </code></pre>
  
  <p>There are 413 of them. I would like to (two ways of saying the same thing):</p>
  
  <ul>
  <li><p>stratify them into 9 groups (arranged in a grid) based on the latitude AND longitude.</p></li>
  <li><p>draw gridlines like a tic-tac-toe board on a plot of lat vs lon and divide the points into bins (stratify) based on the grid cell they fall into.</p></li>
  </ul>
  
  <p>If I wanted to just divide the latitude into 9 groups, I could use the <code>cut</code> function <a href="https://stackoverflow.com/questions/31391350/r-creating-grid-and-checking-what-row-falls-into-each-cell-with-for-loop?noredirect=1&amp;lq=1">like this</a>, but I'm essentially looking for a two-dimensional version of <code>cut</code>.</p>
  
  <p>EDIT:</p>
  
  <p>Using the suggestion: how can I plot this?</p>
  
  <pre><code>paste(cut(lat, 3, labels=FALSE), cut(lon, 3, labels=FALSE))
    [1] "3 3" "3 3" "3 3" "3 1" "3 2" "3 2" "3 3" "3 3" "3 2" "3 1" "3 2" "3 1" "3 2"
   [14] "3 3" "3 3" "3 1" "3 3" "3 2" "3 2" "3 2" "3 1" "3 3" "3 1" "3 1" "3 3" "3 2"
   [27] "3 2" "3 2" "3 1" "3 2" "3 1" "3 3" "3 1" "3 3" "3 1" "3 2" "3 3" "3 2" "3 2"
   [40] "3 3" "3 3" "3 2" "3 2" "3 2" "3 3" "3 1" "3 3" "3 3" "3 3" "3 2" "3 3" "3 3"
   [53] "3 2" "3 2" "3 3" "3 3" "3 1" "3 2" "3 1" "3 2" "3 2" "3 2" "3 3" "3 2" "3 3"
   [66] "3 3" "3 3" "3 3" "3 3" "3 3" "3 3" "3 1" "3 2" "3 3" "3 1" "3 1" "3 1" "3 1"
   [79] "3 2" "3 2" "3 2" "3 1" "3 3" "3 2" "3 2" "3 2" "3 3" "3 3" "3 1" "3 3" "3 1"
   [92] "3 3" "3 3" "3 1" "3 3" "3 1" "3 3" "3 1" "3 2" "3 3" "3 3" "3 2" "3 2" "3 1"
  [105] "3 1" "3 3" "3 2" "3 2" "3 3" "3 3" "3 3" "3 2" "3 1" "3 1" "3 2" "3 2" "3 2"
  [118] "3 1" "3 1" "3 2" "3 3" "3 2" "3 2" "3 3" "3 2" "3 1" "3 3" "3 3" "3 1" "3 3"
  [131] "3 1" "3 1" "3 3" "2 2" "2 2" "2 1" "2 1" "2 2" "2 3" "2 1" "2 2" "2 2" "2 3"
  [144] "2 1" "2 2" "2 3" "2 3" "2 2" "2 3" "2 3" "2 2" "2 2" "2 3" "2 2" "2 1" "2 2"
  [157] "2 2" "2 3" "2 3" "2 1" "2 1" "2 2" "2 1" "2 1" "2 1" "2 3" "2 2" "2 3" "2 3"
  [170] "2 3" "2 2" "2 3" "2 3" "2 2" "2 1" "2 1" "2 1" "2 2" "2 2" "2 2" "2 2" "2 2"
  [183] "2 3" "2 1" "2 2" "2 2" "2 3" "2 3" "2 2" "2 2" "2 3" "2 2" "2 2" "2 2" "2 1"
  [196] "2 3" "2 1" "2 2" "2 3" "2 3" "2 1" "2 3" "2 3" "2 1" "2 2" "2 1" "2 2" "2 3"
  [209] "2 1" "2 3" "2 2" "2 2" "2 2" "2 3" "2 2" "2 1" "2 2" "2 2" "2 3" "2 3" "2 3"
  [222] "2 2" "2 3" "2 2" "2 1" "2 1" "2 2" "2 2" "2 3" "2 2" "2 3" "2 2" "2 2" "2 1"
  [235] "2 2" "2 2" "2 3" "2 2" "2 3" "2 3" "2 3" "2 3" "2 1" "2 1" "2 2" "2 2" "2 3"
  [248] "2 1" "2 2" "2 3" "2 2" "2 3" "2 3" "2 1" "2 1" "2 3" "2 3" "2 1" "2 3" "2 1"
  [261] "2 1" "2 1" "2 3" "2 1" "2 2" "2 2" "2 2" "2 3" "2 3" "2 1" "2 1" "2 2" "2 3"
  [274] "2 3" "2 2" "2 2" "2 1" "1 2" "1 2" "1 3" "1 3" "1 1" "1 1" "1 2" "1 2" "1 2"
  [287] "1 2" "1 1" "1 3" "1 3" "1 2" "1 1" "1 1" "1 1" "1 2" "1 1" "1 1" "1 3" "1 2"
  [300] "1 2" "1 2" "1 3" "1 1" "1 3" "1 1" "1 3" "1 2" "1 1" "1 2" "1 2" "1 2" "1 1"
  [313] "1 3" "1 1" "1 1" "1 2" "1 3" "1 1" "1 2" "1 1" "1 2" "1 1" "1 3" "1 2" "1 2"
  [326] "1 1" "1 2" "1 3" "1 3" "1 1" "1 2" "1 3" "1 3" "1 1" "1 3" "1 3" "1 1" "1 2"
  [339] "1 2" "1 2" "1 3" "1 1" "1 2" "1 3" "1 2" "1 3" "1 3" "1 1" "1 2" "1 2" "1 1"
  [352] "1 1" "1 2" "1 2" "1 3" "1 3" "1 1" "1 2" "1 2" "1 3" "1 1" "1 2" "1 2" "1 3"
  [365] "1 1" "1 2" "1 1" "1 3" "1 3" "1 1" "1 1" "1 2" "1 2" "1 3" "1 1" "1 3" "1 1"
  [378] "1 3" "1 3" "1 1" "1 1" "1 2" "1 3" "1 2" "1 1" "1 2" "1 3" "1 3" "1 2" "1 2"
  [391] "1 3" "1 1" "1 2" "1 2" "1 3" "1 2" "1 2" "1 3" "1 1" "1 3" "1 1" "1 2" "1 2"
  [404] "1 2" "1 1" "1 3" "1 1" "1 2" "1 1" "1 1" "1 1" "1 3" "1 1"
  &gt; 
  </code></pre>
  
  <p>The problem is that the latitude and longitude must be in the same grid section. I might be wrong, but it doesn't look like that's happening here. </p>
  
  <p>EDIT 2: Something's going wrong...getting NAs.</p>
  
  <pre><code>&gt; df2 &lt;- data.frame(lat, lon)
  &gt; df2 &lt;- within(df2, {
  +   grp.lat = cut(lat, (0:3)/3, labels = FALSE)
  +   grp.lon = cut(lon, (0:3)/3, labels = FALSE)
  + })
  &gt; head(df2)
         lat       lon grp.lon grp.lat
  1 43.25724 -96.01955      NA      NA
  2 43.25724 -95.98172      NA      NA
  3 43.25724 -95.92336      NA      NA
  4 43.25616 -96.40973      NA      NA
  5 43.25616 -96.25733      NA      NA
  6 43.25616 -96.17735      NA      NA
  </code></pre>
  
  <p>FINAL SOLUTION:</p>
  
  <pre><code>#Divide the dataset into equally-sized chunks, as evenly as possible, for 9 chunks (coarse)
  
  df2 &lt;- data.frame(lat, lon)
  df2 &lt;- within(df2, {
    grp.lat = cut(lat, 3, labels = FALSE)
    grp.lon = cut(lon, 3, labels = FALSE)
  })
  head(df2)
  
  #Want the minimum lon value for which grp.lon = 1 and the maximum lon value for which grp.lon=1
  
  start_grp1_lon &lt;- min(df2$lon[df2$grp.lon==1])
  start_grp2_lon &lt;- min(df2$lon[df2$grp.lon==2])
  start_grp3_lon &lt;- min(df2$lon[df2$grp.lon==3])
  
  start_grp1_lat &lt;- min(df2$lat[df2$grp.lat==1])
  start_grp2_lat &lt;- min(df2$lat[df2$grp.lat==2])
  start_grp3_lat &lt;- min(df2$lat[df2$grp.lat==3])
  
  plot(lat ~ lon, data = df2, pch = (15:23)[grp.lon*grp.lat], col=[grp.lon]) #pch = (15:17)[grp.lon], col = grp.lat)
  abline(v = c(start_grp1_lon, start_grp2_lon, start_grp3_lon))
  abline(h = c(start_grp1_lat, start_grp2_lat, start_grp3_lat))
  </code></pre>