[R] Reshape or Plyr?

arun smartpink111 at yahoo.com
Sun Apr 21 19:04:56 CEST 2013


HI,

dat1<- structure(list(Location_name = c("079-f2p1-Acetuna", "079-f2p1-Acetuna", 
"079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", 
"079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", 
"079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", 
"079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", 
"079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", 
"079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", 
"079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", 
"079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", 
"079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", 
"079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna", "079-f2p1-Acetuna"
), SPEC_CODE = c("Buzz", "Buzz", "Eumspp", "Frag", "Molspp", 
"Molspp", "Phyllo", "Ptedav", "Ptegym", "Ptepar", "Rhotum", "Sacbil", 
"Saclep", "Buzz", "Buzz", "Molmol", "Molspp", "Molspp", "Nocalb", 
"Phyllo", "Ptedav", "Ptegym", "Ptepar", "Pteper", "Rhotum", "Sacbil", 
"Saclep", "Buzz", "Buzz", "Eumspp", "Molspp", "Molspp", "Myokea", 
"Nocalb", "Phyllo", "Ptedav", "Ptegym", "Ptepar", "Pteper", "Rhotum", 
"Sacbil", "Saclep"), Start_Day = c("2/14/2012", "2/14/2012", 
"2/14/2012", "2/14/2012", "2/14/2012", "2/14/2012", "2/14/2012", 
"2/14/2012", "2/14/2012", "2/14/2012", "2/14/2012", "2/14/2012", 
"2/14/2012", "2/15/2012", "2/15/2012", "2/15/2012", "2/15/2012", 
"2/15/2012", "2/15/2012", "2/15/2012", "2/15/2012", "2/15/2012", 
"2/15/2012", "2/15/2012", "2/15/2012", "2/15/2012", "2/15/2012", 
"2/16/2012", "2/16/2012", "2/16/2012", "2/16/2012", "2/16/2012", 
"2/16/2012", "2/16/2012", "2/16/2012", "2/16/2012", "2/16/2012", 
"2/16/2012", "2/16/2012", "2/16/2012", "2/16/2012", "2/16/2012"
), Survey_Time = c(12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 
12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 
12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.1, 12.2, 12.2, 
12.2, 12.2, 12.2, 12.2, 12.2, 12.2, 12.2, 12.2, 12.2, 12.2, 12.2, 
12.2, 12.2), AI = c(1L, 1L, 1L, 18L, 5L, 5L, 2L, 1L, 1L, 2L, 
6L, 6L, 11L, 2L, 2L, 1L, 7L, 7L, 6L, 1L, 1L, 4L, 4L, 3L, 7L, 
2L, 6L, 1L, 1L, 4L, 2L, 2L, 1L, 4L, 1L, 1L, 1L, 3L, 1L, 17L, 
3L, 15L), Std_AI = c(0.8264463, 0.8264463, 0.8264463, 14.87603, 
4.132231, 4.132231, 1.652893, 0.8264463, 0.8264463, 1.652893, 
4.958678, 4.958678, 9.090909, 1.652893, 1.652893, 0.8264463, 
5.785124, 5.785124, 4.958678, 0.8264463, 0.8264463, 3.305785, 
3.305785, 2.479339, 5.785124, 1.652893, 4.958678, 0.8196721, 
0.8196721, 3.278688, 1.639344, 1.639344, 0.8196721, 3.278688, 
0.8196721, 0.8196721, 0.8196721, 2.459016, 0.8196721, 13.93443, 
2.459016, 12.29508)), .Names = c("Location_name", "SPEC_CODE", 
"Start_Day", "Survey_Time", "AI", "Std_AI"), class = "data.frame", row.names = c(NA, 
-42L))
library(plyr)
res<- mutate(ddply(dat1,.(SPEC_CODE,Location_name),function(x) colSums(x[,-c(1:3,6)])),SumAIbySumST=Survey_Time/AI,Std.SumAIbySumST=10*SumAIbySumST)


res
#   SPEC_CODE    Location_name Survey_Time AI SumAIbySumST Std.SumAIbySumST
#1       Buzz 079-f2p1-Acetuna        72.8  8    9.1000000        91.000000
#2     Eumspp 079-f2p1-Acetuna        24.3  5    4.8600000        48.600000
#3       Frag 079-f2p1-Acetuna        12.1 18    0.6722222         6.722222
#4     Molmol 079-f2p1-Acetuna        12.1  1   12.1000000       121.000000
#5     Molspp 079-f2p1-Acetuna        72.8 28    2.6000000        26.000000
#6     Myokea 079-f2p1-Acetuna        12.2  1   12.2000000       122.000000
#7     Nocalb 079-f2p1-Acetuna        24.3 10    2.4300000        24.300000
#8     Phyllo 079-f2p1-Acetuna        36.4  4    9.1000000        91.000000
#9     Ptedav 079-f2p1-Acetuna        36.4  3   12.1333333       121.333333
#10    Ptegym 079-f2p1-Acetuna        36.4  6    6.0666667        60.666667
#11    Ptepar 079-f2p1-Acetuna        36.4  9    4.0444444        40.444444
#12    Pteper 079-f2p1-Acetuna        24.3  4    6.0750000        60.750000
#13    Rhotum 079-f2p1-Acetuna        36.4 30    1.2133333        12.133333
#14    Sacbil 079-f2p1-Acetuna        36.4 11    3.3090909        33.090909
#15    Saclep 079-f2p1-Acetuna        36.4 32    1.1375000        11.375000


A.K.

----- Original Message -----
From: Bruce Miller <batsncats at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Saturday, April 20, 2013 9:55 AM
Subject: [R] Reshape or Plyr?

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-help at r-project.org 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