merge.xts不使用R合并所有数据 [英] merge.xts not merging all data using R

查看:52
本文介绍了merge.xts不使用R合并所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对以下xts系列进行重新分类,因为第1-8列是字符并且应该是数字,第9-10列是字符,因为他们应该是.....

I am trying to reclass the following xts series since columns 1-8 are character and are suppose to be numeric, columns 9-10 are character as they are suppose to be.....

# data
x <- structure(c(NA, NA, "41.95", "30.55", "29.05", "23.71", NA, "23.80", 
NA, NA, "18.67", NA, "16.90", "17.10", "14.90", "13.64", "12.70", 
"11.65", "10.75", " 9.75", " 9.05", " 7.95", " 6.70", " 6.02", 
" 5.05", NA, NA, " 0.00", " 0.00", " 0.28", "-0.29", NA, " 0.00", 
NA, NA, "-1.28", NA, "-1.10", " 0.00", "-0.30", "-1.51", "-1.50", 
"-2.66", "-1.50", "-1.45", "-1.15", "-0.45", "-0.75", "-1.38", 
"-0.45", "48.20", "43.20", "38.20", "33.20", "28.25", "23.30", 
"22.25", "21.25", "20.30", "19.35", "18.35", "17.40", "16.35", 
"15.50", "14.50", "13.55", "12.55", "11.55", "10.60", " 9.65", 
" 8.65", " 7.70", " 6.80", " 5.90", " 5.00", "48.80", "43.80", 
"38.80", "33.80", "28.65", "23.65", "22.80", "21.65", "20.65", 
"19.65", "18.65", "17.70", "16.70", "15.65", "14.70", "13.70", 
"12.65", "11.75", "10.75", " 9.80", " 8.80", " 7.85", " 6.95", 
" 6.00", " 5.10", "     0", "     0", "    21", "    27", "     0", 
"   356", "     0", "    82", "     0", "     0", "   323", "     0", 
"   444", "   242", "   223", "  1304", "   362", "   263", "   126", 
"   690", "  1445", "   624", "   476", "   995", "   730", NA, 
NA, NA, NA, "   71", "  131", NA, NA, NA, NA, "  435", NA, "   42", 
NA, "  171", "  423", "   83", "   39", "   20", "    6", "  124", 
"   42", "  177", "  425", "  344", " 65.00", " 70.00", " 75.00", 
" 80.00", " 85.00", " 90.00", " 91.00", " 92.00", " 93.00", " 94.00", 
" 95.00", " 96.00", " 97.00", " 98.00", " 99.00", "100.00", "101.00", 
"102.00", "103.00", "104.00", "105.00", "106.00", "107.00", "108.00", 
"109.00", NA, NA, "  0.00", "  0.00", "  0.97", " -1.21", NA, 
"  0.00", NA, NA, " -6.42", NA, " -6.11", "  0.00", " -1.97", 
" -9.97", "-10.56", "-18.59", "-12.24", "-12.95", "-11.27", " -5.36", 
"-10.07", "-18.65", " -8.18", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "Sep 25, 2015", "Sep 25, 2015", "Sep 25, 2015", 
"Sep 25, 2015", "Sep 25, 2015", "Sep 25, 2015", "Sep 25, 2015", 
"Sep 25, 2015", "Sep 25, 2015", "Sep 25, 2015", "Sep 25, 2015", 
"Sep 25, 2015", "Sep 25, 2015", "Sep 25, 2015", "Sep 25, 2015", 
"Sep 25, 2015", "Sep 25, 2015", "Sep 25, 2015", "Sep 25, 2015", 
"Sep 25, 2015", "Sep 25, 2015", "Sep 25, 2015", "Sep 25, 2015", 
"Sep 25, 2015", "Sep 25, 2015"), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", index = structure(c(1442534400, 
1442534400, 1442534400, 1442534400, 1442534400, 1442534400, 1442534400, 
1442534400, 1442534400, 1442534400, 1442534400, 1442534400, 1442534400, 
1442534400, 1442534400, 1442534400, 1442534400, 1442534400, 1442534400, 
1442534400, 1442534400, 1442534400, 1442534400, 1442534400, 1442534400
), tzone = "UTC", tclass = "Date"), .Dim = c(25L, 10L), .Dimnames = list(
    NULL, c("p", "c", "b", "a", "oi", "vol", "strike", "cp", 
    "callput", "expiry")))

我所做的是通过使用以下命令将第1-8列转换为数字:

What I have done is turned columns 1-8 into numeric by using the following:

xx<-reclass(apply(x [,1:8],2,as.numeric),x)

,但是当我尝试将其与 x 中的最后两个字符列(称为 expiry callput )组合时,会将字符列转换为 NA

but when I try to combine it with the last two character columns in x called expiry and callput it turns the character columns into NA

xy<-merge.xts(xx,x [,9:10])

我该如何解决?

推荐答案

xts 是表面下的矩阵,因此它必须全部为数字或全部字符.对于财务应用程序,通常必须是数字,因此问题就变成了如何处理字符列.

xts is a matrix beneath the surface, so it must be all numeric, or all character. With financial applications it normally has to be numeric, so the question becomes what to do with the character columns.

如果字符数据列只能是几个可能的值之一,那么您实际上就有一个因数.您的致电/投放列符合以下条件:

If a character data column can only be one of a few possible values then you actually have a factor. Your call/put column fits this:

as.numeric( factor( c("C","C","P"), levels=c("C","P" ) ) )  #1 1 2

显然,您需要提前了解所有因子水平.

Obviously, you need to know in advance all your factor levels.

如果字符列实际上是日期戳,例如您的选项到期列,则有两种方法可以将其转换为数字.一种是直接使用 as.numeric :

If a character column is actually a datestamp, such as your option expiry column, then there are two ways to convert it to a number. One is to use as.numeric directly:

as.numeric(as.Date("Sep 25, 2015", "%b %d, %Y"))  #16699

另一个是8位YYYYMMDD号码:

The other is as an 8-digit YYYYMMDD number:

as.numeric(format(as.Date("Sep 25, 2015", "%b %d, %Y"), "%Y%m%d"))  #20150921

我更喜欢后者,因为它更具可读性.(但如果要直接在上面进行日期算术,则为前者.)

I prefer the latter, as it is more readable. (But the former if you want to do date arithmetic directly on it.)

时间戳和一天中的时间可以用相同的方式处理.

Timestamps, and time of day can be handled in the same way.

如果您的字符串不适合上述情况,则选择起来不太可口:

If you have a character string that does not fit the above cases, the choices are less palatable:

  • 使用 data.frame .(行名中的Datestamps;您仍然可以 rbind 中的新行; subset 将感兴趣的列放到 xts 满足您的需求
  • 使用并行的 xts 对象(令人讨厌的代码味道).
  • xts对象上的属性.(您可以在 xts 对象上具有一个属性,该属性是 xts 对象.当字符串仅用于主要日期戳的子集时,我使用了此属性xts对象.)
  • 等待xts类,该类允许编写混合类型.:-)
  • Use a data.frame. (Datestamps in the rownames; you can still rbind new rows in; subset out the columns of interest into an xts object when that is what you need)
  • Use parallel xts objects (nasty code smell).
  • An attribute on your xts object. (You can have an attribute on an xts object that is an xts object. I've used this when the character strings are only for a subset of the datestamps in the main xts object.)
  • Wait for the xts class that allows mixed types to be written. :-)

这篇关于merge.xts不使用R合并所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆