I am pretty new to R but have lots of Python experience. I have a data frame that I need to restructure through aggregation. I do have a strategy for a function that will accomplish this. My question is, is there anything built into R, or one of the R packages that will do this kind of aggregation?
The data is historical stock information, the structure is such as:
Ticker, time, quantity, price, type
APPL, timestamp1, 100, 144, BUY
APPL, timestamp2, 100, 145, BUY
APPL, timestamp3, 50, 150, SELL
APPL, timestamp4, 150, 155, SELL
I need to convert this so it is a TRADE record, rather than a BUY/SELL record such as:
Ticker, enter_time, exit_time, enter_price,exit_price
APPL, timestamp1, timestamp4,144.5,153.75
Note that the enter price and exit price are the weighted average buy/sell price. Also, there can be many more entries for buy/sell. The "trade" is finished when everything that has been bought is sold. And of course there will be buying and selling of many tickers intertwined.