I have two data frames -- one with stock closing prices arranged by date (rows) and ticker symbol (columns):
> head(data.stocks)
date A AAPL ABAT AB ABV
1 2010-10-04 32.59 278.64 3.65 26.42 125.89
2 2010-10-05 33.04 288.94 3.66 27.10 129.05
3 2010-10-06 32.67 289.19 3.59 26.99 129.90
4 2010-10-07 33.20 289.22 3.66 27.04 129.94
5 2010-10-08 33.80 294.07 3.84 26.76 132.66
6 2010-10-11 33.75 295.36 3.87 26.95 133.37
The other data frame has open interest (oi) arranged by one row per ticker symbol per date:
> head(data.oi)
date symbol oi close
1 2010-10-04 A 6 0
2 2010-10-04 AA 104 0
3 2010-10-04 AAPL 940 0
4 2010-10-04 AB 0 0
5 2010-10-04 ABAT 0 0
6 2010-10-04 ABB 0 0
I would like to add another column to this second data frame so that I end up with a "panel" data frame with one ticker symbol per date per row with oi and closing price.
I thought this loop would work, but I get the following error:
> for (i in seq(length(data.oi$date))) {
+ row <- which(data.stocks$date == data.oi$date[i])
+ col <- which(colnames(data.stocks) == data.oi$symbol[i])
+ data.oi$close[i] <- data.stocks[row, col]
+ }
Error in data.oi$close[i] <- data.stocks[row, col] :
replacement has length zero
FWIW, it stores the first value in data.oi
, then fails:
> head(data.oi)
date symbol oi close
1 2010-10-04 A 6 32.59
2 2010-10-04 AA 104 0.00
3 2010-10-04 AAPL 940 0.00
4 2010-10-04 AB 0 0.00
5 2010-10-04 ABAT 0 0.00
6 2010-10-04 ABB 0 0.00
Thanks! I feel like reshape
or aggregate
could do it, but I can't figure out how to merge these two.