[R] Reshape or Plyr?
arun
smartpink111 at yahoo.com
Mon Apr 22 02:18:35 CEST 2013
Hi Bruce,
May be this helps.
library(plyr)
res<-mutate(ddply(dat1,.(SPEC_CODE,Location_name),function(x) colSums(x[,-c(1:3,6)])),RA=10*(AI/Survey_Time))
res
# SPEC_CODE Location_name Survey_Time AI RA
#1 Buzz 079-f2p1-Acetuna 72.8 8 1.0989011
#2 Eumspp 079-f2p1-Acetuna 24.3 5 2.0576132
#3 Frag 079-f2p1-Acetuna 12.1 18 14.8760331
#4 Molmol 079-f2p1-Acetuna 12.1 1 0.8264463
#5 Molspp 079-f2p1-Acetuna 72.8 28 3.8461538
#6 Myokea 079-f2p1-Acetuna 12.2 1 0.8196721
#7 Nocalb 079-f2p1-Acetuna 24.3 10 4.1152263
#8 Phyllo 079-f2p1-Acetuna 36.4 4 1.0989011
#9 Ptedav 079-f2p1-Acetuna 36.4 3 0.8241758
#10 Ptegym 079-f2p1-Acetuna 36.4 6 1.6483516
#11 Ptepar 079-f2p1-Acetuna 36.4 9 2.4725275
#12 Pteper 079-f2p1-Acetuna 24.3 4 1.6460905
#13 Rhotum 079-f2p1-Acetuna 36.4 30 8.2417582
#14 Sacbil 079-f2p1-Acetuna 36.4 11 3.0219780
#15 Saclep 079-f2p1-Acetuna 36.4 32 8.7912088
write.csv(res,file="BruceFile.csv",row.names=FALSE)
A.K.
----- Original Message -----
From: Bruce Miller <batsncats at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc:
Sent: Sunday, April 21, 2013 6:18 PM
Subject: Re: [R] Reshape or Plyr?
Hi Arun,
Your first example is what looks like the correct one but needs the
Location_name as in your 2nd example.
I do want to group by "Location_name". The full data set will include up
to 100 Location_names so having this along with SPEC_CODE, Survey_Time,
AI and RA is great.
Then I need to add the line to write the results to a CSV or tab
delimited file.
No need for the Start_Day as in your 2nd example.
On 4/21/2013 5:38 PM, arun wrote:
> Hi Bruce,
>
> From your second email, I think you solved the problem. But, still there is confusion due to the wordings.
> res1<-mutate(ddply(dat1,.(SPEC_CODE),function(x) colSums(x[,-c(1:3,6)])),RA=10*(AI/Survey_Time))
> res1
> SPEC_CODE Survey_Time AI RA
> 1 Buzz 72.8 8 1.0989011
> 2 Eumspp 24.3 5 2.0576132
> 3 Frag 12.1 18 14.8760331
> 4 Molmol 12.1 1 0.8264463
> 5 Molspp 72.8 28 3.8461538
> 6 Myokea 12.2 1 0.8196721
> 7 Nocalb 24.3 10 4.1152263
> 8 Phyllo 36.4 4 1.0989011
> 9 Ptedav 36.4 3 0.8241758
> 10 Ptegym 36.4 6 1.6483516
> 11 Ptepar 36.4 9 2.4725275
> 12 Pteper 24.3 4 1.6460905
> 13 Rhotum 36.4 30 8.2417582
> 14 Sacbil 36.4 11 3.0219780
> 15 Saclep 36.4 32 8.7912088
>
>
> #or
> dat1$RA<-10*with(dat1,ave(AI,SPEC_CODE,FUN=sum))/with(dat1,ave(Survey_Time,SPEC_CODE,FUN=sum))
> head(dat1)
> # Location_name SPEC_CODE Start_Day Survey_Time AI Std_AI RA
> #1 079-f2p1-Acetuna Buzz 2/14/2012 12.1 1 0.8264463 1.098901
> #2 079-f2p1-Acetuna Buzz 2/14/2012 12.1 1 0.8264463 1.098901
> #3 079-f2p1-Acetuna Eumspp 2/14/2012 12.1 1 0.8264463 2.057613
> #4 079-f2p1-Acetuna Frag 2/14/2012 12.1 18 14.8760300 14.876033
> #5 079-f2p1-Acetuna Molspp 2/14/2012 12.1 5 4.1322310 3.846154
> #6 079-f2p1-Acetuna Molspp 2/14/2012 12.1 5 4.1322310 3.846154
>
>
>
> ________________________________
> From: Bruce Miller <batsncats at gmail.com>
> To: arun <smartpink111 at yahoo.com>
> Sent: Sunday, April 21, 2013 4:19 PM
> Subject: Re: [R] Reshape or Plyr?
>
>
>
> Hi Arun,
>
> This looks good; however the not quite correct yet.
>
> Looks like the mutate option may be a good place to start.
> Sorry I did not clearly explain this in my original query. For
> adjustment by unit effort I need the "Base AI per hour" summed for
> the location. Once I have that it can be standardized by 10
> hours. An example for a single from your output
>
> #9 Ptedav 079-f2p1-Acetuna 36.4 3 12.1333333 121.333333
> Is not the correct way to determine AI per hour. So total survey hours for a 3 nights is 36.4 then the AI value is 3 so the AI per hour for Ptedav would be3/36.4 = .08241 then standardized for 10 hours of survey time would be 0.824175824.
> Now I need to determine how to re-arrange your code to reflect the
> Summed AI/Summed survey time.
>
> res<- mutate(ddply(dat1,.(SPEC_CODE,Location_name),function(x) colSums(x[,-c(1:3,6)])),SumAIbySumST=Survey_Time/AI,Std.SumAIbySumST=10*SumAIbySumST)
> Thanks again for your input and help.
>
> Bruce
>
--
Bruce W. Miller, Ph.D.
Conservation Ecologist
Neotropical Bat Projects
office details
11384 Alpine Road
Stanwood, Mi. 49346
Phone (231) 679-6059
More information about the R-help
mailing list