[R] Aggregate on identical link attributes
LCOG1
jroll at lcog.org
Wed Nov 17 00:57:23 CET 2010
Okay here is a solution that works in less than 60 minutes but i feel likes
its messy, if anyone has an alternative solution i would very much
appreciate your insights.
#Create test data
TNode<-c(1:20,21)
FNode<-c(rev(1:20),22)
Volume<-c(rep(100,20),200)
ClassCode=c(rep("Local",20),rep("Freeway",1))
#Create data frame with test data
EmmeData..<-data.frame(TNode=TNode,FNode=FNode,Volume=Volume,ClassCode=ClassCode)
#Create vectors to establish loops
TNodes<-unique(ED..$TNode)
FNodes<-unique(ED..$FNode)
EmmeData..$TF<-paste(EmmeData..$TNode,EmmeData..$FNode,sep="-")
EmmeData..$FT<-paste(EmmeData..$FNode,EmmeData..$TNode,sep="-")
#Split string into 2 elements to paste back together in reverse order as a
matching ID
EmmeData..$F<-unlist(lapply(strsplit(EmmeData..$FT,split="-"), "[", 1))
EmmeData..$T<-unlist(lapply(strsplit(EmmeData..$FT,split="-"), "[", 2))
EmmeData..$Match<-paste(EmmeData..$F,EmmeData..$T,sep="-")
z<-list()
Nd<-EmmeData..$TF
for(nd in Nd){
x<-EmmeData..[EmmeData..$TF==nd,]
y<-EmmeData..[EmmeData..$Match==x$TF,]
if(length(y$Volume)==0){
z[[nd]]<-list(ID=x$TF,Volume=x$Volume,Class=x$ClassCode)
}
if(length(y$Volume)==1){
z[[nd]]<-list(ID=x$TF,Volume=x$Volume+y$Volume,Class=x$ClassCode)
}
}
CalcVolumes.. <- do.call('rbind', z)
--
View this message in context: http://r.789695.n4.nabble.com/Aggregate-on-identical-link-attributes-tp3044009p3045949.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list