title: ddply: how to include a character vector in result
body: <p>sorry, for the cryptic title i didn't find any better summary for my problem. So here's my problem: i have a dataframe and want to make <code>diff()</code> over groups which works fine:</p>
  
  <pre><code> df &lt;- data.frame (name = rep(c("a", "b", "c"), 4),
                index = rep(c("c1", "c2"), each=6),
                year = rep(c(2008:2010),4),
                value = rep(1:3, each=4))
  
  head(df)
  
    name index year value
  
  1    a    c1 2008     1
  2    b    c1 2009     1
  3    c    c1 2010     1
  
  ddply(df, .(name, year), summarize,  value=diff(value))
  </code></pre>
  
  <p>However, I would like to include the <code>index</code> in my result wich i tried to do with:</p>
  
  <pre><code>ddply(df, .(name, year), summarize,  value=diff(value), index=index)
  </code></pre>
  
  <p>Yet this yields the error message:</p>
  
  <pre><code>length(rows) == 1 is not TRUE
  </code></pre>
  
  <p>Which is I guess because the index has more rows because it is not processed by <code>diff</code>. Is there a quick solution to my problem?</p>
  
  <p>Thank you very much!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I try to clarify my question what I want to add to the result:</p>
  
  <p>Suppose the variable <code>index</code> above. This is a factor that ought to explain something. Yet, I cannot take <code>diff()</code> of it that would not make sense so I just want to pass this one without changing anything. I tried <code>drop==FALSE</code> wich did yield the same error messsage.</p>
  
  <p>Sorr for all this confusion! Here's a very simple example:</p>
  
  <pre><code>name year  index  value
   a   2008    c1    10
   a   2009    c2    30
   a   2010    c1    40
  </code></pre>
  
  <p>after taking diff's acroos group 'a' this looks like:</p>
  
  <pre><code>name year index d.value 
   a   2009  c2     +20  #c2 stayed the same just the first row got intentionally dropped.
   a   2010  c1     +10
  </code></pre>
  
  <p>consider the unfortunate name <code>index</code> as something like an attribute: it can change during the years but would not make sense to take a <code>diff()</code></p>
  
  <p>I really really hope this gives you a clue what I want - if not I'll delete the question because I found an unelegant workaround ;) and sorry for all the inconvenience!</p>