简单 R 任务:在指定行处将指定列除以 1000

发布于 2024-12-09 13:49:44 字数 1331 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧幽蓝 2024-12-16 13:49:44

我不熟悉 OHLC 数组,但假设索引方法有效:

relevantRows<-index(stock) < "2007-07-20"

一旦你有了一个包含所有相关行的向量(实际上是一个逻辑向量,对于应该被改变),你可能可以像这样简单地使用它:

stock$Open[relevantRows]<-stock$Open[relevantRows]/1000

有可能(取决于 OHLC 数组的内部结构),即使这样也有效:

stock[relevantRows, c("Open", "High", "Low", "Close")]<-stock[relevantRows, c("Open", "High", "Low", "Close")]/1000

I'm not familiar with OHLC arrays, but assuming that index method works:

relevantRows<-index(stock) < "2007-07-20"

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:

stock$Open[relevantRows]<-stock$Open[relevantRows]/1000

It is possible (depending on the internals of OHLC arrays), that even this works:

stock[relevantRows, c("Open", "High", "Low", "Close")]<-stock[relevantRows, c("Open", "High", "Low", "Close")]/1000
暗恋未遂 2024-12-16 13:49:44

如果日期不在 20/7/2007 之前,则 (index(stock)<"2007-07-20")FALSE,因此 ( index(stock)<"2007-07-20")*1000 结果为零。您的 Inf 值是除以零的结果。

你可以试试这个:

stock[index(stock) < "2007-07-20", -5] <- stock[index(stock) < "2007-07-20", -5] / 1000

If the date is not before 20/7/2007, then (index(stock)<"2007-07-20") is FALSE and so (index(stock)<"2007-07-20")*1000 comes out as zero. Your Inf values are a result of dividing by zero.

You could try this:

stock[index(stock) < "2007-07-20", -5] <- stock[index(stock) < "2007-07-20", -5] / 1000
郁金香雨 2024-12-16 13:49:44

您可以使用 TTR 包中的 adjRatios 函数来执行此操作。看起来您已经有了一个 xts 对象,所以这就是我使用的:

library(quantmod)
x <- structure(c(6111.01, 6145, 6120.01, 6240, 6231, 6279, 6145, 6190, 
6250, 6285, 6325, 6295, 6102.7, 5960, 6120, 6225.1, 6231, 6180.01, 
6145, 6135.05, 6225, 6261, 6270, 6190, 956, 8771, 10579, 8882, 
8015, 8461), .Dim = c(6L, 5L), .Dimnames = list(NULL, c("Open", 
"High", "Low", "Close", "Volume")), index = structure(c(1041746400, 
1042005600, 1042092000, 1042178400, 1042437600, 1042524000), tzone = "",
tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = "")
s <- xts(1/1000,as.Date("2003-01-10"))
r <- adjRatios(s,,Cl(x))
OHLC(x) * drop(r[,"Split"]) * drop(r[,"Div"])

如果您使用的是来自雅虎财经的数据,那么您可以使用 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:

library(quantmod)
x <- structure(c(6111.01, 6145, 6120.01, 6240, 6231, 6279, 6145, 6190, 
6250, 6285, 6325, 6295, 6102.7, 5960, 6120, 6225.1, 6231, 6180.01, 
6145, 6135.05, 6225, 6261, 6270, 6190, 956, 8771, 10579, 8882, 
8015, 8461), .Dim = c(6L, 5L), .Dimnames = list(NULL, c("Open", 
"High", "Low", "Close", "Volume")), index = structure(c(1041746400, 
1042005600, 1042092000, 1042178400, 1042437600, 1042524000), tzone = "",
tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = "")
s <- xts(1/1000,as.Date("2003-01-10"))
r <- adjRatios(s,,Cl(x))
OHLC(x) * drop(r[,"Split"]) * drop(r[,"Div"])

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文