Question

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)
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top