-1

I have two lists. One is a list that only defines an instrument name. The second is the running total of that instrument. There are multiple entries in the instrument name list that are the same. I have a Hash that aggregates/prints the final number of the running total for each instrument in the instrument list. I am trying to print the new totals for only the insrtuments that have changed, and specifically print the change quantity, if that is at all possible, from a given point. For example, I would like to find the changes in instruments from the positionFile only after a certain line and print only the last change amount. This is basically a change in inventory fetcher that prints only the changes in inventory from a given point. Any help at all to accomplish this would be incredible. I am having trouble getting the starting point to reference as well as how to reference the last inventory count before the new running total is printed, among many other things. I am working in R and the instrument list and running total(positionList) come from a single file called positionFile. The lists are:

 library(hash)
 instrumentList = positionFile[,2]
 positionList = positionFile[,3]
 posHash = hash(instrumentList,positionList)
Karsten W.
  • 17,826
  • 11
  • 69
  • 103
  • For me, a sample of `positionFile` would be helpful to understand your question. – Karsten W. Feb 23 '14 at 22:01
  • One thing that might help is that the lists are derived from a file called 'Inventory_MMDDYYYY' If i can somehow reference InventoryX versus InventoryX-1 and only display the change in new InventoryX quantities that would also do the trick. – user3311286 Feb 23 '14 at 22:02
  • PositionFile is a data.frame that has the following 11 columns and about 10k rows at this point: Time Security Quantity CostBasts RealPnL Val1 Val2 UpdateReason Tag Expiration SettlementRule – user3311286 Feb 23 '14 at 22:04
  • I can get the last item of the positionFile by using this command: `last_item = dim(positionFile)[1]` But how could I either 1) Ignore the 9377 rows before it and not know the change in running total, or 2) reference the last running total quantity from an above row in the data.frame when a new item is added? – user3311286 Feb 23 '14 at 22:29

1 Answers1

0

Not sure if I understood your question, but maybe a "split-apply-combine" approach will work:

positionList <- data.frame(instrument=c("A", "B", "C", "A", "B", "C", "A", "B", "C"), runtotal=c(1,1,1,2,0,3,3,-1,4), stringsAsFactors=FALSE)
sp <- split(positionList, positionList$instrument)
fun <- function(dat) {
    if(nrow(dat)>1)
        dat[nrow(dat),"runtotal"] - dat[nrow(dat)-1, "runtotal"]
    else
        NA
}
app <- lapply(sp, FUN=fun)
res <- do.call(rbind, app)

Now res should contain the last change of the running total.

Karsten W.
  • 17,826
  • 11
  • 69
  • 103