Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know you asked for a "plyr"-specific solution, but for the sake of sharing, here is an alternative approach in base R. In my opinion, I find the base R approach just as "readable". And, at least in this particular case, it's a lot faster!</p> <pre><code>output &lt;- within(df1, { yoy &lt;- ave(ab, team, lg, FUN = function(x) c(NA, diff(x))) }) head(output) # year lg team ab yoy # 1 1884 UA ALT 108 NA # 2 1997 AL ANA 1703 NA # 3 1998 AL ANA 1502 -201 # 4 1999 AL ANA 660 -842 # 5 2000 AL ANA 85 -575 # 6 2001 AL ANA 219 134 library(rbenchmark) benchmark(DDPLY = { ddply(df1, .(team, lg), mutate , yoy = c(NA, diff(ab))) }, WITHIN = { within(df1, { yoy &lt;- ave(ab, team, lg, FUN = function(x) c(NA, diff(x))) }) }, columns = c("test", "replications", "elapsed", "relative", "user.self")) # test replications elapsed relative user.self # 1 DDPLY 100 10.675 4.974 10.609 # 2 WITHIN 100 2.146 1.000 2.128 </code></pre> <h2>Update: <code>data.table</code></h2> <p>If your data are very large, check out <code>data.table</code>. Even with this example, you'll find a good speedup in relative terms. Plus the syntax is super compact and, in my opinion, easily readable.</p> <pre><code>library(plyr) df1 &lt;- aggregate(ab~year+lg+team, FUN=sum, data=baseball) library(data.table) DT &lt;- data.table(df1) DT # year lg team ab # 1: 1884 UA ALT 108 # 2: 1997 AL ANA 1703 # 3: 1998 AL ANA 1502 # 4: 1999 AL ANA 660 # 5: 2000 AL ANA 85 # --- # 2523: 1895 NL WSN 839 # 2524: 1896 NL WSN 982 # 2525: 1897 NL WSN 1426 # 2526: 1898 NL WSN 1736 # 2527: 1899 NL WSN 787 </code></pre> <p>Now, look at this concise solution:</p> <pre><code>DT[, yoy := c(NA, diff(ab)), by = "team,lg"] DT # year lg team ab yoy # 1: 1884 UA ALT 108 NA # 2: 1997 AL ANA 1703 NA # 3: 1998 AL ANA 1502 -201 # 4: 1999 AL ANA 660 -842 # 5: 2000 AL ANA 85 -575 # --- # 2523: 1895 NL WSN 839 290 # 2524: 1896 NL WSN 982 143 # 2525: 1897 NL WSN 1426 444 # 2526: 1898 NL WSN 1736 310 # 2527: 1899 NL WSN 787 -949 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload