问题描述
我有两个数据框.一个由三个变量组成,分别是date"、strike"和vol",每天观察 20 次,每月观察 100 次,每年观察 1200 次(交易日),看起来像这样
I have two data frames.The one consists of three variables, namely "date", "strike" and "vol" with 20 observations a day, 100 a month and 1200 a year (in trading days), which looks like this
Date Price Vol
2008-09-01 20 0.2
2008-09-01 30 0.5
...
因此,对于每个月,我都有一定的价格和成交量值,范围分别为 10 到 40、0.1 到 0.7.
第二个包括来自第一个的内插值.所以我没有日期了,但是其他变量的小步骤:
So for each month I have certain values for price and vol, ranging from 10 to 40, 0.1 to 0.7, respectively.
The second one includes interpolated values from the first one. So I do not have the date anymore, however small steps for the other variables:
Price Vol
20 0.2
21 0.21
22 0.24
30 0.5
因此,虽然一帧显示离散时间的值,但另一帧或多或少具有连续性.
现在我的问题是:如何告诉 R 将第二个数据帧合并到第一个数据帧中,接管两个离散数据之间的连续价格/成交量的日期,以获得这样的结果:
So, while one frame shows values in a discrete time, the other one is more or less of continuous nature.
Now my question: how is it possible to tell R to merge the second data frame into the first one, taking over the dates for the continuous prices/vols between the two discrete ones, to get to something like this:
Date Price Vol
2008-09-01 20 0.2
2008-09-01 21 0.21
2008-09-01 22 0.24
...
2008-09-01 30 0.5
我就是不知道该怎么做.我总是以不再按升序排列的日期结束 NA 值.
I just cannot figure out how to do it. I always ended up with NA values for the dates which are no longer in ascending order.
非常感谢您的支持
丹妮
Thank you very much for your support
Dani
推荐答案
我完全错过了第一篇文章的重点.这个做日期.但我同意 Shane 的观点,除非某些下游功能需要数据帧,否则时间序列是个好主意.
I totally missed the point with the first post. This one does the date. But I agree with Shane that unless some downstream function requires data frames, then a time series is a good idea.
A <- data.frame(date=rep("2001-05-25", 2), price=c(20, 30), vol=c(0.2, 0.5))
B <- data.frame(price=seq(min(A$price), max(A$price), by=1))
C <- merge(A, B, all=TRUE)
index <- which(!is.na(C$vol))
for (i in seq(nrow(A))[-1]) {
C$date[index[i-1]:index[i]] <- rep(A$date[i-1], A$price[i] - A$price[i-1] + 1)
C$vol[index[i-1]:index[i]] <- seq(A$vol[i-1], A$vol[i], length=(A$price[i] - A$price[i-1] + 1))
}
ans <- C[, c(2, 1, 3)]
ans
date price vol
1 2001-05-25 20 0.20
2 2001-05-25 21 0.23
3 2001-05-25 22 0.26
4 2001-05-25 23 0.29
5 2001-05-25 24 0.32
6 2001-05-25 25 0.35
7 2001-05-25 26 0.38
8 2001-05-25 27 0.41
9 2001-05-25 28 0.44
10 2001-05-25 29 0.47
11 2001-05-25 30 0.50
这篇关于在 R 中合并两个不同的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!