title: How to apply a function only to the first row of a group in dplyr?
body: <h1>Data</h1>
  
  <p>I have 3 variables. <code>Vehicle.ID2</code> is a unique ID of a vehicle pair, <code>dV</code> is the speed difference in lead and following vehicles and <code>dA</code> is the acceleration difference which remains constant for some time. So, my grouping variables are <code>Vehicle.ID2</code> and <code>dA</code>. Following are few rows of original data for only 1 <code>Vehicle.ID2</code>:  </p>
  
  <pre><code>    veh &lt;- structure(list(Vehicle.ID2 = c("907-904", "907-904", "907-904", 
  "907-904", "907-904", "907-904", "907-904", "907-904", "907-904", 
  "907-904", "907-904", "907-904", "907-904", "907-904", "907-904", 
  "907-904", "907-904", "907-904", "907-904", "907-904", "907-904"
  ), dA = c(0.43024, 0.43024, 0.43024, 0.43024, 0.43024, 0.43024, 
  0.43024, 0.43024, 0.43024, 0.43024, 0.43024, -0.3162, -0.3162, 
  -0.3162, -0.3162, -0.3162, -0.3162, -0.3162, -0.3162, -0.3162, 
  -0.3162), dV = c(-0.0427200000000001, 0.11031, 0.22627, 0.30058, 
  0.33838, 0.35264, 0.35803, 0.36481, 0.37677, 0.39292, 0.40961, 
  0.42206, 0.42557, 0.416090000000001, 0.39003, 0.34668, 0.296580000000001, 
  0.268000000000001, 0.29681, 0.399859999999999, 0.554639999999999
  )), class = "data.frame", .Names = c("Vehicle.ID2", "dA", "dV"
  ), row.names = c(NA, -21L))
  </code></pre>
  
  <h1>Goal</h1>
  
  <p>I want to create a new column <code>OC_DV</code>. Initially all values of OC_DV are <code>"no"</code>. This I can do with:</p>
  
  <pre><code>veh$OC_DV &lt;- "no"  
  </code></pre>
  
  <p>Now, first I want to split the data by the variables <code>Vehicle.ID2</code> and <code>dA</code>. Then for every group I want to see if the sign of the first value of <code>dV</code> matches with the sign of the last value of <code>dV</code>. Based on the conditions of sign match or mismatch, I want to modify only the FIRST value of <code>OC_DV</code>. Following is the code:</p>
  
  <pre><code>OC_DV[1] &lt;- ifelse(sign(head(dV,1))== sign(tail(dV,1)),  "no",
                       ifelse(sign(head(dV,1))==-1 &amp; sign(tail(dV,1))==1, "OPDV",
                              ifelse(sign(head(dV,1))==1 &amp; sign(tail(dV,1))==-1,"CLDV","no")))  
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>I tried using <code>mutate</code> and <code>do</code> but they produce errors:   </p>
  
  <pre><code>    veh &lt;- veh %&gt;% 
    group_by(Vehicle.ID2, dA) %&gt;%
    mutate(OC_DV[1] = ifelse(sign(head(dV,1))== sign(tail(dV,1)),  "no",
                             ifelse(sign(head(dV,1))==-1 &amp; sign(tail(dV,1))==1, "OPDV",
                                    ifelse(sign(head(dV,1))==1 &amp; sign(tail(dV,1))==-1,"CLDV","no")))
    )
  Error: unexpected '=' in:
  "  group_by(Vehicle.ID2, dA) %&gt;%
    mutate(OC_DV[1] ="
  
  
  
   veh &lt;- veh %&gt;% 
    group_by(Vehicle.ID2, dA) %&gt;%
    do(OC_DV[1] = ifelse(sign(head(dV,1))== sign(tail(dV,1)),  "no",
                             ifelse(sign(head(dV,1))==-1 &amp; sign(tail(dV,1))==1, "OPDV",
                                    ifelse(sign(head(dV,1))==1 &amp; sign(tail(dV,1))==-1,"CLDV","no")))
    )
  Error: unexpected '=' in:
  "  group_by(Vehicle.ID2, dA) %&gt;%
    do(OC_DV[1] ="
  </code></pre>
  
  <p>If I drop <code>[1]</code> there is no error but then all values in a group are changed:</p>
  
  <pre><code>    veh %&gt;% 
    group_by(Vehicle.ID2, dA) %&gt;%
    mutate(OC_DV = ifelse(sign(head(dV,1))== sign(tail(dV,1)),  "no",
                                  ifelse(sign(head(dV,1))==-1 &amp; sign(tail(dV,1))==1, "OPDV",
                                         ifelse(sign(head(dV,1))==1 &amp; sign(tail(dV,1))==-1,"CLDV","no")))
    )
  </code></pre>
  
  <p>What can I do to change only the first value?</p>
  
  <h1>Desired Output:</h1>
  
  <pre><code>    structure(list(Vehicle.ID2 = c("907-904", "907-904", "907-904", 
  "907-904", "907-904", "907-904", "907-904", "907-904", "907-904", 
  "907-904", "907-904", "907-904", "907-904", "907-904", "907-904", 
  "907-904", "907-904", "907-904", "907-904", "907-904", "907-904"
  ), dA = c(0.43024, 0.43024, 0.43024, 0.43024, 0.43024, 0.43024, 
  0.43024, 0.43024, 0.43024, 0.43024, 0.43024, -0.3162, -0.3162, 
  -0.3162, -0.3162, -0.3162, -0.3162, -0.3162, -0.3162, -0.3162, 
  -0.3162), dV = c(-0.0427200000000001, 0.11031, 0.22627, 0.30058, 
  0.33838, 0.35264, 0.35803, 0.36481, 0.37677, 0.39292, 0.40961, 
  0.42206, 0.42557, 0.416090000000001, 0.39003, 0.34668, 0.296580000000001, 
  0.268000000000001, 0.29681, 0.399859999999999, 0.554639999999999
  ), OC_DV = c("OPDV", "no", "no", "no", "no", "no", "no", "no", 
  "no", "no", "no", "no", "no", "no", "no", "no", "no", "no", "no", 
  "no", "no")), class = "data.frame", .Names = c("Vehicle.ID2", 
  "dA", "dV", "OC_DV"), row.names = c(NA, -21L))
  </code></pre>