Hello R community, I need to do some aggregation based on the test data below. The below code works ok, but when its applied to my real data which includes over 9,000 records the process runs for over an hour. I know there is a more efficient way of doing this. I want to Sum the below data's volumes where FNODE and TNODE match, i.e. FNODE=1 and TNODE =20 -> Volume=100 , FNODE=20 and TNODE =1 -> Volume=100 These should be aggregated toTotalVolume= 200. Also, there are some link without partner links(see record 21 in test data). This record should not be summed with another link since there isnt a compliment to sum with. Thanks in advance. -JR #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 list to store results LinkSum_<-list() #Create vecotrs to establish loops TNodes<-unique(EmmeData..$TNode) FNodes<-unique(EmmeData..$FNode) for(tn in TNodes){ for(fn in FNodes){ TF<-EmmeData..[EmmeData..$TNode==tn & EmmeData..$FNode==fn,] FT<-EmmeData..[EmmeData..$TNode==fn & EmmeData..$FNode==tn,] if(length(TF$TNode)>0){ LinkSum_[[tn]]<-list(ID=list(c(TF$TNode,TF$FNode)),ClassCode=TF$ClassCode,TotalVolume=TF$Volume+FT$Volume) } } } -- View this message in context: http://r.789695.n4.nabble.com/Aggregate-on-identical-link-attributes-tp3044009p3044009.html Sent from the R help mailing list archive at Nabble.com.
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.
Possibly Parallel Threads
- [LLVMdev] local variable in Pattern definition?
- re: o2hb_do_disk_heartbeat:963 ERROR: Device "sdb1" another node is heartbeating in our slot!
- [PATCH v3 06/14] RDMA/hfi1: Use mmu_interval_notifier_insert for user_exp_rcv
- [PATCH v2 06/15] RDMA/hfi1: Use mmu_range_notifier_inset for user_exp_rcv
- [LLVMdev] CFG as DOT: where do node addresses come from?