有没有更好的方法来重塑数据帧数据?
temp <- bdh(conn,c("AUDUSD Curncy","EURUSD Curncy"),"PX_LAST","20110101")
给出
head(temp)
ticker date PX_LAST
1 AUDUSD Curncy 2011-01-01 NA
2 AUDUSD Curncy 2011-01-02 NA
3 AUDUSD Curncy 2011-01-03 1.0205
4 AUDUSD Curncy 2011-01-04 1.0040
5 AUDUSD Curncy 2011-01-05 1.0014
6 AUDUSD Curncy 2011-01-06 0.9969
和
tail(temp)
ticker date PX_LAST
2127 EURUSD Curncy 2013-11-26 1.3557
2128 EURUSD Curncy 2013-11-27 1.3570
2129 EURUSD Curncy 2013-11-28 1.3596
2130 EURUSD Curncy 2013-11-29 1.3591
2131 EURUSD Curncy 2013-11-30 NA
2132 EURUSD Curncy 2013-12-01 NA
换句话说,数据只是相互垂直固定,需要进一步处理才能使它们正常工作。如何将这些数据重新组合成各种代码,即
head(temp)
AUDUSD.Curncy EURUSD.Curncy
2011-01-01 NA NA
2011-01-02 NA NA
2011-01-03 1.0205 1.3375
2011-01-04 1.0040 1.3315
2011-01-05 1.0014 1.3183
2011-01-06 0.9969 1.3028
我用谷歌搜索的所有重塑问题都没有我想要的那种重塑。我已经实现了下面给出的自己的零碎解决方案,但为了学习,我想问你们,如果有一个更优雅的解决方案吗?
答案 0 :(得分:5)
您可以尝试read.zoo
。使用index.column
指定存储列索引/时间,并根据split
columnn重新整形数据。结果是zoo
时间序列
library(zoo)
z <- read.zoo(text = "ticker date PX_LAST
1 AUDUSD 2011-01-01 NA
2 AUDUSD 2011-01-02 NA
3 AUDUSD 2011-01-03 1.0205
4 AUDUSD 2011-01-04 1.0040
5 AUDUSD 2011-01-05 1.0014
6 AUDUSD 2011-01-06 0.9969
2127 EURUSD 2013-11-26 1.3557
2128 EURUSD 2013-11-27 1.3570
2129 EURUSD 2013-11-28 1.3596
2130 EURUSD 2013-11-29 1.3591
2131 EURUSD 2013-11-30 NA
2132 EURUSD 2013-12-01 NA", index.column = "date", split = "ticker")
z
# AUDUSD EURUSD
# 2011-01-01 NA NA
# 2011-01-02 NA NA
# 2011-01-03 1.0205 NA
# 2011-01-04 1.0040 NA
# 2011-01-05 1.0014 NA
# 2011-01-06 0.9969 NA
# 2013-11-26 NA 1.3557
# 2013-11-27 NA 1.3570
# 2013-11-28 NA 1.3596
# 2013-11-29 NA 1.3591
# 2013-11-30 NA NA
# 2013-12-01 NA NA
str(z)
答案 1 :(得分:1)
这正是我们创建RbbgExtension包的原因。它是Rbbg软件包的一个包装器,它在处理财务数据时处理许多问题 - 我们在日常工作中遇到的问题,包括金融机构的回溯交易策略等。
正如您所看到的输出是xts对象,但如果查询跨越多个代码和多个字段,那么输出将是一个数组 - 但您可以阅读文档中的原因。
我们已将软件包开源并在GitHub上公开发布。只需使用哈德利的开发工具&#39;函数install_github(&#34; pgarnry / RbbgExtension&#34;)获取包。它有一些依赖关系,包括&#34; Rbbg&#34;。
> require(RbbgExtension)
Loading required package: RbbgExtension
>
> tickers <- c("AUDUSD", "EURUSD")
>
> prices <- HistData(tickers = tickers,
+ type = "Curncy",
+ fields = "PX_LAST",
+ startdate = "20110101")
R version 3.1.2 (2014-10-31)
rJava Version 0.9-6
Rbbg Version 0.5.3
Java environment initialized successfully.
Looking for most recent blpapi3.jar file...
Adding C:\blp\API\APIv3\JavaAPI\v3.7.1.1\lib\blpapi3.jar to Java classpath
Bloomberg API Version 3.7.1.1
> class(prices)
[1] "xts" "zoo"
> head(prices)
AUDUSD EURUSD
2011-01-03 1.0168 1.3361
2011-01-04 1.0051 1.3308
2011-01-05 0.9995 1.3149
2011-01-06 0.9944 1.3003
2011-01-07 0.9959 1.2907
2011-01-10 0.9956 1.2951
> tail(prices)
AUDUSD EURUSD
2015-01-26 0.7925 1.1238
2015-01-27 0.7937 1.1381
2015-01-28 0.7889 1.1287
2015-01-29 0.7762 1.1320
2015-01-30 0.7762 1.1291
2015-02-02 0.7806 1.1351
答案 2 :(得分:-1)
bdhx <- function(conn,securities,start_date,end_date=NULL,fields="PX_LAST",override_fields = NULL,overrides = NULL) {
temp <- bdh(conn=conn,securities=securities,fields=fields,start_date=start_date,end_date=end_date,override_fields=override_fields)
if (colnames(temp)[1]=="date")
{temp <- as.xts(temp)[,-1];colnames(temp) <- securities;res <- temp;}
else
{cn <- unique(temp[,1]);fil <- temp[,1]==cn[1];
res <- xts(temp[fil,3],as.Date(temp[fil,2]));colnames(res) <- securities[1];
for (i in 4:(length(cn)+2)){
fil <- temp[,1]==cn[i-2]
temp2 <- xts(temp[fil,3],as.Date(temp[fil,2]));colnames(temp2) <- securities[i-2];
res <- merge.xts(res,temp2)}
}
res}