在R中查找并显示特定范围的行: “`R 查找并显示特定范围的行 “`

huangapple go评论292阅读模式
英文:

look up and display only a certain range of rows in R

问题

我想知道如何基于固定值显示某个范围的行:

y <- c(1, 59, 60, 65, 85, 86, 1550, 97)
z <- c(1:8)
test <- cbind(y, z)
test

所以说,假设我需要只看从85开始的+/-2行,有没有办法从表格中提取?理想情况下,我想要的是这样的:

在R中查找并显示特定范围的行:
“`R
查找并显示特定范围的行
“`

谢谢你的帮助。

Peter编写的更新版本:

#所需库

library(quantmod)
library(pedquant)

#从雅虎财经下载期权链,使用示例股票代码“BABA”和到期日“2023-06-23”
ticker <- c("BABA")
options <- getOptionChain(ticker, Exp = "2023-06-23")

#分离看涨和看跌期权,并仅显示行权价、最后价格和成交量
call_option <- options$calls[, c("Strike", "Last", "Vol")]
put_option <- options$puts[, c("Strike", "Last", "Vol")]

#获取更新后的股票价格以筛选平价期权水平
ticker_price <- md_stock(ticker, type = "real")
price <- ticker_price$close

view_window <- function(dfr, var, x, pm) {
  
  idx <- which(abs(dfr[, var] - x) == min(abs(dfr[, var] - x)))
  
  return(dfr[(idx - pm):(idx + pm), ])
  
}

view_window(dfr = call_option,
            var = "Strike",
            x = price,
            pm = 3)

在这个示例中,数据框[dfr]是call_option,var是call_option$Strike,x是交易价格,但会显示"Error in test[, var] : subscript out of bounds"错误消息,我相信数据框的格式可能有问题,函数应该能够正常工作。有任何想法吗?非常感谢。

英文:

I am wondering how do I display a certain range of rows based on a fixed value:

y&lt;-c(1,59,60,65,85,86,1550,97)
z&lt;-c(1:8)
test&lt;-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

So say, it just so happens that I need to see only +/-2 rows from 85, is there a way I can extract from the table? Ideally, I would like to have something like this:

在R中查找并显示特定范围的行:
“`R
查找并显示特定范围的行
“`

Thanks a lot for helping.

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 &quot;BABA&quot; &amp; exp= &quot;2023-06-23&quot;
ticker&lt;-c(&quot;BABA&quot;)
options&lt;-getOptionChain(ticker,Exp=&quot;2023-06-23&quot;)

# seperate call/put options and display only Strike, Last Price, Volume
call_option&lt;-options$calls[,c(&quot;Strike&quot;,&quot;Last&quot;,&quot;Vol&quot;)]
put_option&lt;-options$puts[,c(&quot;Strike&quot;,&quot;Last&quot;,&quot;Vol&quot;)]

#getting the updated stock price to filter at-the-money level 

ticker_price&lt;-md_stock(ticker,type=&quot;real&quot;)
price&lt;-ticker_price$close

view_window &lt;- function(dfr, var, x, pm){
  
  idx &lt;- 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

In this example, data frame [dfr] is call_option, var is call_option$Strike, x is the traded price, however, an error msg of "Error in test[, var] : subscript out of bounds" is shown, pretty sure something is wrong with the format of my data frame n the function should have been working perfectly. Any ideas? Many thanks.

答案1

得分: 1

你可以使用这样的函数:

# 其中
# dfr 是你的数据框
# var 是你想要使用的带引号的变量名
# x 是你想要以其为中心创建子集的变量值,以及
# pm 是子集的正负范围

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 = test,
            var = "y",
            x = 85,
            pm = 2)
#>         y z
#> [1,]   60 3
#> [2,]   65 4
#> [3,]   85 5
#> [4,]   86 6
#> [5,] 1550 7

函数的限制:
如果 x 在主体向量的第一个或最后一个值的正负索引范围内,该函数将失败。
我没有测试过如果主体列中有多个等于 x 的值会产生什么影响。

创建于 2023-06-21,使用 reprex v2.0.2

英文:

You could use a function like this:

# Where
# dfr is your data frame
# var is the quoted variable name you want to use 
# x is the value of the variable you want to centre your subset on, and 
# pm is the plus and minus range of the subset

view_window &lt;- function(dfr, var, x, pm){

  idx &lt;- which(abs(test[, var]-x) == min(abs(test[, var] - x)))
  
  return(dfr[(idx - pm) : (idx + pm), ])
  
}

view_window(dfr = test,
            var = &quot;y&quot;,
            x = 85,
            pm = 2)
#&gt;         y z
#&gt; [1,]   60 3
#&gt; [2,]   65 4
#&gt; [3,]   85 5
#&gt; [4,]   86 6
#&gt; [5,] 1550 7

Limits of the function:
if x within the plus or minus index of the first or last value in the subject vector the function will fail.
I've not tested the effect if there are multiple values in the subject column which are equal to x.

<sup>Created on 2023-06-21 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月22日 04:46:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定