混合日期数据文件

时间:2016-07-06 22:45:59

标签: r time-series zoo financial

我输入以下数据文件:三对日期价格数据(加上列索引麻木)。问题是每个价格都有不同的国家法定假日,因此英国和美国的价格最终不对齐。有没有一种很好的方法将日期推送到xts / zoo格式并填入价格不存在的console.log("foo is " + fooAge + " years old"); (mkt已关闭)?

NA

ColNumb  Date1      UK2Y       Date2         US2Y       Date3       GBPUSD
1     09/07/2012   0.9330    09/07/2012    0.5210    09/07/2012    1.552554
2    10/07/2012    0.9401    10/07/2012    0.5235    10/07/2012    1.551831
3    11/07/2012    0.9122    11/07/2012    0.5003    11/07/2012    1.550388
4    12/07/2012    0.8732    12/07/2012    0.4805    12/07/2012    1.542972 

我尝试过使用UK2y <- as.xts(data[1:1033,1:2]) US2y <- as.xts(data[,3:4]) GBPUSD <- data[,5:6] ,但这导致了无效的动物园对象。我最终在&#39; A&#39;中找到了正确的格式化日期。作为没有{A <- strptime(UK2y$Date1, format = "%d/%m/%Y")}动物园的POSIX类(&#34;结构错误&#34;):

cbind

您在上面看到的另一个问题是每个配对列的长度不同。某种&#34;日期匹配&#34;函数会减轻,或者在zoo / xts中存在soln?

1 个答案:

答案 0 :(得分:2)

以下是使用colspan的解决方案:

merge

修改

您可以创建一个空数据框,其中包含按所需顺序生成的正确日期,然后合并:

# subset your data
UK2Y = data[,c("Date1", "UK2Y")]
US2Y = data[,c("Date2", "US2Y")]
GBPUSD = data[,c("Date3", "GBPUSD")]

# rename them to have the same Date column
names(UK2Y)[names(UK2Y) == "Date1"] <- "Date"
names(US2Y)[names(US2Y) == "Date2"] <- "Date"
names(GBPUSD)[names(GBPUSD) == "Date3"] <- "Date"

# Test: remove one data 
US2Y = US2Y[-4,] # market closed in US this day

# Merge the data frames
group = merge(UK2Y, US2Y, by = "Date", all = T) # "all = T" will show missing data as NA
group = merge(group, GBPUSD, by = "Date", all = T)

print(group)

    Date   UK2Y   US2Y   GBPUSD
1 2012-07-09 0.9330 0.5210 1.552554
2 2012-07-10 0.9401 0.5235 1.551831
3 2012-07-11 0.9122 0.5003 1.550388
4 2012-07-12 0.8732     NA 1.542972