[R] Reshape or Plyr?

Arhopala dfrcleary at gmail.com
Sun Apr 21 23:16:07 CEST 2013


Hi Bruce

It sounds like the aggregate function is what you need. Here's an example 
using the ChickWeight dataset in R:

chick.agg<-aggregate(ChickWeight[,c('weight', 'Time')], by = 
ChickWeight[c('Chick','Diet')], sum)

weight.time<-chick.agg$weight/chick.agg$Time

chick.agg.df<-data.frame(chick.agg, weight.time)

As you can see above, once you have the aggregated dataframe, you can just 
create another vector (weight.time), which is chick weight divided by time 
and add this to a new database using the data.frame function. Take a look 
at the ChickWeight function so you'll understand what I've done.

cheers,

Danny




On Saturday, April 20, 2013 2:55:03 PM UTC+1, Bruce Miller wrote:
>
> H all, 
>
> I have relative abundance data from >100 sites.  This is from acoustic 
> monitoring and usually the data is for 2-3 nights but in some cases my 
> be longer like months or years for each location.. 
> The data output from my management data base is proved by species by 
> night for each location so data frame would look like this below. What I 
> need to do is sum the Survey_time by Spec_Code for each location name 
> and divide summed AI values for each Spec_code by the summed Survey time 
> to adjust for unit effort then standardize it all by *10 to represent 
> the relative abundance by survey hour to 10 hours. How best to do this?   
> Using Plyr or reshape? 
>
> Location name         SPEC_CODE         Start_Day         Survey_Time 
>         AI         Std AI 
> 079-f2p1-Acetuna         Buzz         2/14/2012         12.1         1 
>         0.8264463 
> 079-f2p1-Acetuna         Buzz         2/14/2012         12.1         1 
>         0.8264463 
> 079-f2p1-Acetuna         Eumspp         2/14/2012         12.1         1 
>         0.8264463 
> 079-f2p1-Acetuna         Frag         2/14/2012         12.1         18 
>         14.87603 
> 079-f2p1-Acetuna         Molspp         2/14/2012         12.1         5 
>         4.132231 
> 079-f2p1-Acetuna         Molspp         2/14/2012         12.1         5 
>         4.132231 
> 079-f2p1-Acetuna         Phyllo         2/14/2012         12.1         2 
>         1.652893 
> 079-f2p1-Acetuna         Ptedav         2/14/2012         12.1         1 
>         0.8264463 
> 079-f2p1-Acetuna         Ptegym         2/14/2012         12.1         1 
>         0.8264463 
> 079-f2p1-Acetuna         Ptepar         2/14/2012         12.1         2 
>         1.652893 
> 079-f2p1-Acetuna         Rhotum         2/14/2012         12.1         6 
>         4.958678 
> 079-f2p1-Acetuna         Sacbil         2/14/2012         12.1         6 
>         4.958678 
> 079-f2p1-Acetuna         Saclep         2/14/2012         12.1         11 
>         9.090909 
> 079-f2p1-Acetuna         Buzz         2/15/2012         12.1         2 
>         1.652893 
> 079-f2p1-Acetuna         Buzz         2/15/2012         12.1         2 
>         1.652893 
> 079-f2p1-Acetuna         Molmol         2/15/2012         12.1         1 
>         0.8264463 
> 079-f2p1-Acetuna         Molspp         2/15/2012         12.1         7 
>         5.785124 
> 079-f2p1-Acetuna         Molspp         2/15/2012         12.1         7 
>         5.785124 
> 079-f2p1-Acetuna         Nocalb         2/15/2012         12.1         6 
>         4.958678 
> 079-f2p1-Acetuna         Phyllo         2/15/2012         12.1         1 
>         0.8264463 
> 079-f2p1-Acetuna         Ptedav         2/15/2012         12.1         1 
>         0.8264463 
> 079-f2p1-Acetuna         Ptegym         2/15/2012         12.1         4 
>         3.305785 
> 079-f2p1-Acetuna         Ptepar         2/15/2012         12.1         4 
>         3.305785 
> 079-f2p1-Acetuna         Pteper         2/15/2012         12.1         3 
>         2.479339 
> 079-f2p1-Acetuna         Rhotum         2/15/2012         12.1         7 
>         5.785124 
> 079-f2p1-Acetuna         Sacbil         2/15/2012         12.1         2 
>         1.652893 
> 079-f2p1-Acetuna         Saclep         2/15/2012         12.1         6 
>         4.958678 
> 079-f2p1-Acetuna         Buzz         2/16/2012         12.2         1 
>         0.8196721 
> 079-f2p1-Acetuna         Buzz         2/16/2012         12.2         1 
>         0.8196721 
> 079-f2p1-Acetuna         Eumspp         2/16/2012         12.2         4 
>         3.278688 
> 079-f2p1-Acetuna         Molspp         2/16/2012         12.2         2 
>         1.639344 
> 079-f2p1-Acetuna         Molspp         2/16/2012         12.2         2 
>         1.639344 
> 079-f2p1-Acetuna         Myokea         2/16/2012         12.2         1 
>         0.8196721 
> 079-f2p1-Acetuna         Nocalb         2/16/2012         12.2         4 
>         3.278688 
> 079-f2p1-Acetuna         Phyllo         2/16/2012         12.2         1 
>         0.8196721 
> 079-f2p1-Acetuna         Ptedav         2/16/2012         12.2         1 
>         0.8196721 
> 079-f2p1-Acetuna         Ptegym         2/16/2012         12.2         1 
>         0.8196721 
> 079-f2p1-Acetuna         Ptepar         2/16/2012         12.2         3 
>         2.459016 
> 079-f2p1-Acetuna         Pteper         2/16/2012         12.2         1 
>         0.8196721 
> 079-f2p1-Acetuna         Rhotum         2/16/2012         12.2         17 
>         13.93443 
> 079-f2p1-Acetuna         Sacbil         2/16/2012         12.2         3 
>         2.459016 
> 079-f2p1-Acetuna         Saclep         2/16/2012         12.2         15 
>         12.29508 
>
>
> Thanks for any suggestions.  Excel will be a mess to try to do that. 
>
> Bruce 
>
>         [[alternative HTML version deleted]] 
>
> ______________________________________________ 
> R-h... at r-project.org <javascript:> mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-help 
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html 
> and provide commented, minimal, self-contained, reproducible code. 
>


More information about the R-help mailing list