Possible Duplicate:
function with multiple outputs
numbering rows within groups in a data frame
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:
place       individual
here        1
here        2
here        3
there       1
there       2
somewhere   1 
somewhere   2
I have written this:
    nest<-data.frame(location=c("one","one","two", "three", "three", "three"))
    individual<- function(x)
      {
        pp = 1
        jj = 1
        for (i in 2:length(x)){
            if (x[i] == x[pp]){
                res<- jj+1
                pp = pp + 1
                jj = jj + 1
            }
            else{
                res<- 1
                pp = pp + 1
                jj = 1
            }
        }
        return(res)
      }
    test<- individual(nest$location)
    test
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.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.
 
     
    