title: How to add serial number for each group in Spark DataFrame
body: <p>I am trying to add a column to my Spark DataFrame with a serial number based on a condition: I would like to assign sequential integers for each group in one of the columns. </p>
  
  <p>I have tried many approaches,but nothing seems to be working.</p>
  
  <pre><code>df&lt;-data.frame(location=c("a","a","d","d","d"),
                 device_id=c(123,3455,234,4565,675),
                 expected_column=c(1,2,1,2,3))
  
  #final_data_visitor is my spark Data Frame 
  random_data&lt;-final_data_visitor
  
  random_data&lt;-random_data %&gt;%
         group_by(location_id,device_subscriber_id)%&gt;%                
         mutate(visit_seq=1:n())
  
  random_data
  # error is "Error in from:to : NA/NaN argument
  In addition: Warning message:
  In 1:n() : NAs introduced by coercion"
  </code></pre>
  
  <p>This is the error I get when I try to execute my code:</p>
  
  <blockquote>
    <p>"Error in from:to : NA/NaN argument
    In addition: Warning message:
    In 1:n() : NAs introduced by coercion</p>
  </blockquote>