我想知道如何根据固定值显示特定范围的行:
y<-c(1,59,60,65,85,86,1550,97)
z<-c(1:8)
test<-cbind(y,z)
test
y z
[1,] 1 1
[2,] 59 2
[3,] 60 3
[4,] 65 4
[5,] 85 5
[6,] 86 6
[7,] 1550 7
[8,] 97 8
那么,碰巧我只需要看到85行中的+/-2行,有没有一种方法可以从表中提取?理想情况下,我希望有这样的东西:
非常感谢你的帮助.
Updated version with new function written by Peter :个
#required libiaries
library(quantmod)
library(pedquant)
# download option chain from yahoo finance, using an example of ticker "BABA" & exp= "2023-06-23"
ticker<-c("BABA")
options<-getOptionChain(ticker,Exp="2023-06-23")
# seperate call/put options and display only Strike, Last Price, Volume
call_option<-options$calls[,c("Strike","Last","Vol")]
put_option<-options$puts[,c("Strike","Last","Vol")]
#getting the updated stock price to filter at-the-money level
ticker_price<-md_stock(ticker,type="real")
price<-ticker_price$close
view_window <- function(dfr, var, x, pm){
idx <- which(abs(test[, var]-x) == min(abs(test[, var] - x)))
return(dfr[(idx - pm) : (idx + pm), ])
}
view_window(dfr=call_option,
var=call_option$Strike,
x=price,pm=3)
Error in test[, var] : subscript out of bounds
在本例中,数据帧[dfr]是CALL_OPTION,var是CALL_OPTION$STRING,x是交易价格,但是,显示了错误消息"Error in test[,var]:下标Out Out Bound",非常确定我的数据帧的格式有问题n函数应该工作得很好.有什么主意吗?非常感谢.