我想知道如何根据固定值显示一定范围的行:
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行,有没有一种方法可以从表中提取?理想情况下,我希望有这样的东西:
谢谢你的帮助。
更新版本,新增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
在该示例中, Dataframe [dfr]是call_option,var是call_option$Strike,x是交易价格,然而,错误消息“Error in test[,var]:下标越界”显示,非常肯定的东西是错误的格式,我的 Dataframe n的功能应该已经工作得很好。有什么想法吗?非常感谢。
1条答案
按热度按时间g0czyy6m1#
你可以使用这样的函数:
函数的限制:如果x在主题向量中的第一个或最后一个值的正或负索引内,则函数将失败。我还没有测试如果subject列中有多个值等于x时的效果。
创建于2023-06-21使用reprex v2.0.2