简单 R 任务:在指定行处将指定列除以 1000
我有一个 OHLC 股票报价数组想要处理。
Open High Low Close Volume
2003-01-05 6111.01 6145.00 6102.70 6145.00 956
2003-01-08 6145.00 6190.00 5960.00 6135.05 8771
2003-01-09 6120.01 6250.00 6120.00 6225.00 10579
2003-01-10 6240.00 6285.00 6225.10 6261.00 8882
2003-01-13 6231.00 6325.00 6231.00 6270.00 8015
2003-01-14 6279.00 6295.00 6180.01 6190.00 8461
公司在给定日期进行了拆分,因此我需要将该日期之前的所有开盘价、最高价、最低价、收盘价列除以 1000。 当我现在正在学习 R 基础知识时,我想为这个任务找出很好的 R 解决方案。 我编写的最好的代码是(无法找出如何应用于给定的列,stock$Open 不起作用):
apply(stock, 2, function(stock) stock/((index(stock)<"2007-07-20")*1000) )
但是,结果很奇怪,其中许多都是 inf:
2006-10-26 Inf Inf Inf Inf Inf
2006-10-27 Inf Inf Inf Inf Inf
2006-10-30 Inf Inf Inf Inf Inf
2006-10-31 Inf Inf Inf Inf Inf
2006-11-01 Inf Inf Inf Inf Inf
2006-11-02 Inf Inf Inf Inf Inf
2006-11-03 Inf Inf Inf Inf Inf
2006-11-07 Inf Inf Inf Inf Inf
提前非常感谢!
I have an OHLC array of stock quotes that I want to process.
Open High Low Close Volume
2003-01-05 6111.01 6145.00 6102.70 6145.00 956
2003-01-08 6145.00 6190.00 5960.00 6135.05 8771
2003-01-09 6120.01 6250.00 6120.00 6225.00 10579
2003-01-10 6240.00 6285.00 6225.10 6261.00 8882
2003-01-13 6231.00 6325.00 6231.00 6270.00 8015
2003-01-14 6279.00 6295.00 6180.01 6190.00 8461
The company made a split @ given date, so I need to divide all open,high,low,close columns before that date by 1000.
As I am learning R basics now I want to figure out nice R solution for this task.
The best piece of code I've managed to code is (cant find out how to apply to given cols, stock$Open doesn't work):
apply(stock, 2, function(stock) stock/((index(stock)<"2007-07-20")*1000) )
However, the results are strange, many of them are inf:
2006-10-26 Inf Inf Inf Inf Inf
2006-10-27 Inf Inf Inf Inf Inf
2006-10-30 Inf Inf Inf Inf Inf
2006-10-31 Inf Inf Inf Inf Inf
2006-11-01 Inf Inf Inf Inf Inf
2006-11-02 Inf Inf Inf Inf Inf
2006-11-03 Inf Inf Inf Inf Inf
2006-11-07 Inf Inf Inf Inf Inf
Many thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不熟悉 OHLC 数组,但假设索引方法有效:
一旦你有了一个包含所有相关行的向量(实际上是一个逻辑向量,对于应该被改变),你可能可以像这样简单地使用它:
有可能(取决于 OHLC 数组的内部结构),即使这样也有效:
I'm not familiar with
OHLC array
s, but assuming that index method works:Once you've got a vector holding all the relevant rows (in fact a logical vector that holds TRUE for the rows that should be changed), you can probably use this simply like this:
It is possible (depending on the internals of
OHLC array
s), that even this works:如果日期不在 20/7/2007 之前,则
(index(stock)<"2007-07-20")
为FALSE
,因此( index(stock)<"2007-07-20")*1000
结果为零。您的Inf
值是除以零的结果。你可以试试这个:
If the date is not before 20/7/2007, then
(index(stock)<"2007-07-20")
isFALSE
and so(index(stock)<"2007-07-20")*1000
comes out as zero. YourInf
values are a result of dividing by zero.You could try this:
您可以使用 TTR 包中的 adjRatios 函数来执行此操作。看起来您已经有了一个 xts 对象,所以这就是我使用的:
如果您使用的是来自雅虎财经的数据,那么您可以使用 quantmod 中的
adjustOHLC
函数自动从中提取分割和股息数据雅虎并调整系列。有关更多选项,请参阅?adjustOHLC
。You can use the
adjRatios
function in the TTR package to do this. It looks like you already have an xts object, so that's what I use:If you're using data from Yahoo Finance, then you can use the
adjustOHLC
function in quantmod to automatically pull split and dividend data from Yahoo and adjust the series. See?adjustOHLC
for more options.