mercredi 5 août 2015

if statement with ddply function

I am trying to use the if statement with ddply but am having issues with the if statement.

An example dataset is:

data<-data.frame(Gear=c(rep("S",10),rep("C",10)),TowSurvey=c(0,0,1,1,0,1,1,1,1,0),TowCom=c(0,1,1,1,0,1,1,1,1,0),
    StationID=c(1,2,3,4,5,6,7,8,9,10),Totwght=c(2,8,6,4,12,9,56,7,89,10),Totexpwght=c(5,8,12,45,89,56,23,78,56,41),
    Expnum=c(1,5,6,98,45,2,6,3,7,45),Exp=c(56,25,85,74,1,23,56,45,89,75))

My first try was

if(data$Gear=="S" & data$TowSurvey== 1 | data$Gear=="C" & data$TowCom== 1){
    datad<-ddply(data, .(StationID,Gear), summarize,Totwghtpertow=sum(Totwght),
    Totexppertow=sum(Totexpwght),Totnum =sum(Expnum),Totexpnum=sum(Exp))}
print(datad)

But the records that don't meet the if statement criteria are included in datad.

Then I found this post: Aggregate (count) rows that match a condition, group by unique values. Aggregate (count) rows that match a condition, group by unique values

So my second attempt based on the answer from the post was

datad<-ddply(data, .(StationID,Gear), summarize,Totwghtpertow=sum(Totwght[Gear=="S" & TowSurvey== 1 | Gear=="C" & TowCom== 1]))

I only tried with one column as a test and am getting the same results. Any help would be appreciated in trying to figure this out. Thanks

Aucun commentaire:

Enregistrer un commentaire