R 将国库券价格(第32位、第64位、第128位)转换为十进制
我希望将美国国债期货价格转换为小数(因此127-01+=127+1/32+1/64,以及127-01 1/4=127+1/32+1/128等) 我曾经在有分数的情况下取得过一些成功,但当分数不符合大数字时,我就失败了 失败的原因是R 将国库券价格(第32位、第64位、第128位)转换为十进制,r,string-matching,R,String Matching,我希望将美国国债期货价格转换为小数(因此127-01+=127+1/32+1/64,以及127-01 1/4=127+1/32+1/128等) 我曾经在有分数的情况下取得过一些成功,但当分数不符合大数字时,我就失败了 失败的原因是strsplit不返回NA或空对象-因此位的顺序会被打乱,因此后续的分数都是错误的 这是我的情况: ty1 <- c("127-03", "126-31", "126-31+", "127-04+", "127-02+", "127-00+") ty2 <-
strsplit
不返回NA
或空对象-因此位的顺序会被打乱,因此后续的分数都是错误的
这是我的情况:
ty1 <- c("127-03", "126-31", "126-31+", "127-04+", "127-02+", "127-00+")
ty2 <- c("127-03", "126-31", "127-04+", "127-02+", "127-00+", "127",
"127-01 1/4", "128-01 3/4")
ty1_dec <- c(127+3/32, 126+31/32, 126+31/32+1/64, 127+4/32+1/64,
127+2/32+1/64, 127+0/32+1/64)
ty2_dec <- c(127+3/32, 126+31/32, 127+04/32+1/64, 127+2/32+1/64,
127+0/32+1/64, 127, 127+1/32+0/64+1/128, 128+1/32+1/64+1/128)
tFrac2Dec <-function(x){
hyphSplit <- strsplit(x, "-")
splitMatx <- matrix(unlist(hyphSplit), ncol=2, byrow=TRUE)
fracs <- t(apply(splitMatx[,2, drop=F], 1, function(X) substring(X, c(1,3), c(2,3))))
splitMatx[,2] <- (as.numeric(fracs[,1]) + ifelse(fracs[,2] == "+", 0.5, 0))/32
fracEval <- function(y){
eval(parse(text=paste(y[1], "+", y[2])))
}
apply(splitMatx,1,fracEval)
}
但当价格只是一个大数字时,它就失败了
> tFrac2Dec(ty2) == ty2_dec
Error in parse(text = paste(y[1], "+", y[2])) :
<text>:1:4: unexpected numeric constant
1: 01 1
^
In addition: Warning message:
In matrix(unlist(hyphSplit), ncol = 2, byrow = TRUE) :
data length [15] is not a sub-multiple or multiple of the number of rows [8]
>tFrac2Dec(ty2)=ty2\u dec
分析错误(text=paste(y[1],“+”,y[2]):
:1:4:意外的数字常量
1: 01 1
^
此外:警告信息:
在矩阵中(unlist(hyphSplit),ncol=2,byrow=TRUE):
数据长度[15]不是行数[8]的次倍数或倍数
我可以看出这是由于第一个strsplit
步骤,但我无法找到解决方案。我已经摆弄了一些if
类型的解决方案,但什么都不起作用
有没有一个简单的方法可以做到这一点 像这样的
tFrac2Dec <- function(x) {
hyphSplit <- strsplit(x, "-")
ch1 <- sapply(hyphSplit,`[`, 1)
ch2 <- sapply(hyphSplit,`[`, 2)
x32 <- as.integer(substring(ch2, 1, 2))
x128 <- rep(0, length(ch1))
x128[grep("\\+$", ch2)] <- 2
x128[grep("1/4", ch2, fixed=TRUE)] <- 1
x128[grep("3/4", ch2, fixed=TRUE)] <- 3
dec <- x32/32 + x128/128
dec[is.na(dec)] <- 0
as.integer(ch1) + dec
}
tFrac2Dec(ty1)
## [1] 127.0938 126.9688 126.9844 127.1406 127.0781 127.0156
tFrac2Dec(ty2)
## [1] 127.0938 126.9688 127.1406 127.0781 127.0156 127.0000 127.0312
tFrac2Dec+1/已接受。天才!刚刚发现了一个新的障碍——一些期货价格如下120-273/4=(120+27/32+1/64+1/128)。你能帮我理解如何推广这段代码来处理这种情况吗?请参阅编辑(未经测试,但我认为它会起作用)。另外,对于未来的读者,请将此添加到原始问题中。修改qn和您的答案以反映解决方案。再次感谢你的帮助。一对过于热心的编辑拒绝了你对我答案的编辑;我无法覆盖它们,但可以将您的编辑复制到我的答案中。谢谢
tFrac2Dec <- function(x) {
hyphSplit <- strsplit(x, "-")
ch1 <- sapply(hyphSplit,`[`, 1)
ch2 <- sapply(hyphSplit,`[`, 2)
x32 <- as.integer(substring(ch2, 1, 2))
x128 <- rep(0, length(ch1))
x128[grep("\\+$", ch2)] <- 2
x128[grep("1/4", ch2, fixed=TRUE)] <- 1
x128[grep("3/4", ch2, fixed=TRUE)] <- 3
dec <- x32/32 + x128/128
dec[is.na(dec)] <- 0
as.integer(ch1) + dec
}
tFrac2Dec(ty1)
## [1] 127.0938 126.9688 126.9844 127.1406 127.0781 127.0156
tFrac2Dec(ty2)
## [1] 127.0938 126.9688 127.1406 127.0781 127.0156 127.0000 127.0312