title: Returning multiple results from loop in R?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8216470/function-with-multiple-outputs">function with multiple outputs</a><br>
    <a href="https://stackoverflow.com/questions/12925063/numbering-rows-within-groups-in-a-data-frame">numbering rows within groups in a data frame</a>  </p>
  </blockquote>
  
  
  
  <p>G'day, I have a list of individuals that are grouped by place. I want to produce a new variable that gives a number to each individual dependant on their place. What I would like my data to look like is:</p>
  
  <pre><code>place       individual
  here        1
  here        2
  here        3
  there       1
  there       2
  somewhere   1 
  somewhere   2
  </code></pre>
  
  <p>I have written this:</p>
  
  <pre><code>    nest&lt;-data.frame(location=c("one","one","two", "three", "three", "three"))
      individual&lt;- function(x)
        {
          pp = 1
          jj = 1
          for (i in 2:length(x)){
              if (x[i] == x[pp]){
                  res&lt;- jj+1
                  pp = pp + 1
                  jj = jj + 1
              }
              else{
                  res&lt;- 1
                  pp = pp + 1
                  jj = 1
              }
          }
          return(res)
        }
  
      test&lt;- individual(nest$location)
      test
  </code></pre>
  
  <p>I am pretty sure this does what I want, however, I can't figure out how to return more than one result value. How, do I change this function to return a result for each value of location? Or, is there an easier way to do this with a pre-existing R package?</p>
  
  <p>P.S. on a side note, I start the function from nest$individual[2] because when it starts the function from 1 and it tries to look for the previous value (which doesn't exist) it returns an error. If anyone has a thought on how to get around this I would love to hear it. Cheers.</p>