如何使用data.table执行以下(直接使用sqldf)并得到完全相同的结果:
library(data.table)
whatWasMeasured <- data.table(start=as.POSIXct(seq(1, 1000, 100),
origin="1970-01-01 00:00:00"),
end=as.POSIXct(seq(10, 1000, 100), origin="1970-01-01 00:00:00"),
x=1:10,
y=letters[1:10])
measurments <- data.table(time=as.POSIXct(seq(1, 2000, 1),
origin="1970-01-01 00:00:00"),
temp=runif(2000, 10, 100))
## Alternative short names for data.tables
dt1 <- whatWasMeasured
dt2 <- measurments
## Straightforward with sqldf
library(sqldf)
sqldf("select * from measurments m, whatWasMeasured wwm
where m.time between wwm.start and wwm.end")
Run Code Online (Sandbox Code Playgroud) 直到最近R升级后奇怪的事情才开始发生,我在Ubuntu 14.04上是一个快乐的StatET用户.与网络操作相关的任何内容都将失败 例如,我无法再从CRAN安装软件包:
> install.packages("Hmisc")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
Error in url("http://cran.r-project.org/CRAN_mirrors.csv") :
internet routines cannot be loaded
In addition: Warning message:
In url("http://cran.r-project.org/CRAN_mirrors.csv") :
unable to load shared object '/usr/lib/R/modules//internet.so':
/usr/lib/R/modules//internet.so: symbol curl_multi_wait, version CURL_OPENSSL_3 not defined in file libcurl.so.4 with link time reference
Run Code Online (Sandbox Code Playgroud)
如果我在终端中运行R,它会顺利运行:
> install.packages("Hmisc")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
trying URL 'http://cran.at.r-project.org/src/contrib/Hmisc_3.16-0.tar.gz'
Content type 'application/x-gzip' length 629536 …Run Code Online (Sandbox Code Playgroud) 我想知道找到xts对象中与一个特定行相同的所有行的最快方法是什么
library(xts)
nRows <- 3
coreData <- data.frame(a=rnorm(nRows), b=rnorm(nRows), c=rnorm(nRows))
testXts1 <- xts(coreData, order.by=as.Date(1:nRows))
testXts2 <- xts(coreData, order.by=as.Date((nRows + 1):(2*nRows)))
testXts3 <- xts(coreData, order.by=as.Date((2*nRows + 1):(3*nRows)))
testXts <- rbind(testXts1, testXts2, testXts3)
> testXts
a b c
1970-01-02 -0.3288756 1.441799 1.321608
1970-01-03 -0.7105016 1.639239 -2.056861
1970-01-04 0.1138675 -1.782825 -1.081799
1970-01-05 -0.3288756 1.441799 1.321608
1970-01-06 -0.7105016 1.639239 -2.056861
1970-01-07 0.1138675 -1.782825 -1.081799
1970-01-08 -0.3288756 1.441799 1.321608
1970-01-09 -0.7105016 1.639239 -2.056861
1970-01-10 0.1138675 -1.782825 -1.081799
rowToSearch <- first(testXts)
> rowToSearch
a …Run Code Online (Sandbox Code Playgroud) 我想使用quantmod 生成这样的情节https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110826/19da3834/attachment.png.
我想,我有点沮丧,这是一项非常简单的任务.我希望能够使用quantmod在图表上绘制一条线.经过几天的尝试,我无处可去.我看不到有关如何使用quantmod :: addLines函数执行此操作的示例(这是我找到的唯一类似问题,但无法找到答案http://r.789695.n4.nabble.com/quantmod- plot-trendline-td894632.html)
我的问题是:我想在指定的日期/时间/条nad y值绘制一条水平线.这条线只允许从某个特定条形开始的n(例如5)条长(另外我还想在指定的y值的行上方添加文本).
我尝试了几件事:
getSymbols("SPY")
lines.SPY <- (Hi(SPY) + Lo(SPY))/2
names(lines.SPY) <- c("lines")
lines.SPY$BuySell <- ifelse(lag(lines.SPY$lines) > lines.SPY$lines, 1, -1)
chartSeries(SPY, subset="2011-08::", theme=chartTheme('white',
up.col='blue', dn.col='red'))
addTA(lines.SPY$lines[lines.SPY$BuySell == -1,], type='p', col='darkred', pch="_", on=1, cex = 2.5)
addTA(lines.SPY$lines[lines.SPY$BuySell == 1,], type='p', col='green4', pch="_", on=1, cex = 2.5)
Run Code Online (Sandbox Code Playgroud)
但这实际上不是线......而且我不知道要添加文字......
然后我试过这个
getSymbols("SPY")
subset = "2011-03::"
dev.new()
chartSeries(SPY, subset=subset, theme="white")
test <- xts(rep(coredata(last(Cl(SPY))), 20), order.by=index(last(SPY, n=20)))
addTA(test, on=1, col="red", legend=NULL, lwd=3)
Run Code Online (Sandbox Code Playgroud)
同样,添加文本是不可能的.这种方法的另一个问题是我无法摆脱顶部的传说.因为我想在一个图表上绘制数十或数百个这样的线条,所以不应该显示图例...
在此先感谢您的想法/代码示例/ ...
最好的问候,萨莫.
我无法解决为什么在简单创建xts对象时出错
xts(rep(0, NROW(TICK.NYSE)), order.by = index(TICK.NYSE))
Error in xts(rep(0, NROW(TICK.NYSE)), order.by = index(TICK.NYSE)) :
order.by requires an appropriate time-based object
Run Code Online (Sandbox Code Playgroud)
14天前,当我上次使用相同的代码时(这时,唯一的区别是TICK.NYSE长度增长,因为自那时起添加了数据).
更多细节如下:
> Sys.getenv("TZ")
[1] "EST"
> tail(xts(rep(0, NROW(TICK.NYSE)), order.by = index(TICK.NYSE)))
Error in xts(rep(0, NROW(TICK.NYSE)), order.by = index(TICK.NYSE)) :
order.by requires an appropriate time-based object
> class(index(TICK.NYSE["T09:30/T09:31"]))
[1] "POSIXct"
> tail(xts(rep(0, NROW(tail(TICK.NYSE))), order.by = index(tail(TICK.NYSE))))
Error in xts(rep(0, NROW(tail(TICK.NYSE))), order.by = index(tail(TICK.NYSE))) :
order.by requires an appropriate time-based object
> tail(TICK.NYSE)
TICK-NYSE.Open TICK-NYSE.High TICK-NYSE.Low TICK-NYSE.Close
2012-03-15 14:54:00 -278 -89 …Run Code Online (Sandbox Code Playgroud) 我正在尝试计算xts对象子集的累积乘积.这是一个我想要的例子,以及使用period.apply或其他一些基于c ++的快速函数可以更快/更优雅的问题吗?
# install.packages("qmao", repos="http://R-Forge.R-project.org")
require(qmao) # for do.call.rbind()
# I need something like cumprod over xts but by endpoints (subsets of xts)
test <- xts(rep(0.01, length(as.Date(13514:13523, origin="1970-01-01"))), as.Date(13514:13523, origin="1970-01-01"))
ep <- c(0, 5, NROW(test))
# This does not do the trick
period.prod(test, INDEX=ep)
# So, try the obvious, but it does not do the trick
period.apply(test, INDEX=ep, FUN=function(x) cumprod(1 + x))
# Well, write your own
# Hm, there is no split.xts that takes ep (endpoints) as parameter...
# …Run Code Online (Sandbox Code Playgroud) 我想使用 RPy2 从 Python 中的 R 包中调用函数。我使用 conda 安装了 RPy2 并意识到它在 conda 中安装了 R 的新副本......我不想要那个。我只想拥有并使用一个 R,这是 /usr/lib/R 中的默认值。
怎么做?如何强制 conda 和 Python 以及 RPy2 使用安装在 /usr/lib/R 中的默认 R?
我正在使用新的chart_Series,add_TA非常多.它对我很有效,我发现它非常有用.
我想在图表上添加一些东西(水平线和一些文字).这里出现了问题.正确绘制水平线和文本后,如果我调用后续文件,它们就会消失add_TA...请参阅下面的示例代码,它会重现问题:
library(quantmod)
getSymbols("SPY")
dev.new()
chart_Series(SPY)
add_TA(ADX(HLC(SPY))$ADX)
abline(h=15, col="red")
abline(h=35, col="green")
text(10, 7, "Text and horizontal lines disappear after next add_TA is called",
col="blue", cex=0.8, adj = c(0,0))
# run the code up to this point (including text(...
# see how horizontal lines drawn with abline and text is displayed correctly
# now run the last line by adding additional TA and you will see that lines
# and text disappears
add_TA(DVI(Cl(SPY))$dvi)
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?
编辑:如何使这项工作(根据下面的约书亚评论:正在重绘绘图对象(chob)时重绘行和文本)?
在Joshua对我之前关于这个"问题"的帖子做出的回答后(quantmod add_TA和chart_Series的问题 - 调用下一个add_TA后行和文本消失了)我在quantmod中添加了一些"扩展",可以绘制行/段和文本.这样做时我遇到了一个无法解决的问题:
require(quantmod)
add_VerticalLine<-function(xCoordinatesOfLines, on=1, ...) {
lenv <- new.env()
lenv$add_verticalline <- function(x, xCoordinatesOfLines, ...) {
xdata <- x$Env$xdata
xsubset <- x$Env$xsubset
xcoords <- seq(1:NROW(xdata[xsubset]))[index(xdata[xsubset]) %in% index(xCoordinatesOfLines[xsubset])]
abline(v=xcoords, ...)
}
mapply(function(name, value) {assign(name,value,envir=lenv)}, names(list(xCoordinatesOfLines=xCoordinatesOfLines, ...)), list(xCoordinatesOfLines=xCoordinatesOfLines, ...))
exp <- parse(text=gsub("list","add_verticalline", as.expression(substitute(list(x=current.chob(),
xCoordinatesOfLines=xCoordinatesOfLines, ...)))), srcfile=NULL)
plot_object <- current.chob()
lenv$xdata <- plot_object$Env$xdata
plot_object$set_frame(sign(on)*abs(on)+1L)
plot_object$add(exp,env=c(lenv, plot_object$Env),expr=TRUE)
plot_object
}
datesForLines <- c("2012-02-06", "2012-02-07")
verticalLines.xts <- xts(rep(0, length(datesForLines)), order.by=as.Date(datesForLines))
SPX <- getSymbols("^GSPC", from="2012-01-01", auto.assign=FALSE)
chart_Series(SPX, subset="2012")
add_VerticalLine(verticalLines.xts, …Run Code Online (Sandbox Code Playgroud) 我想在quantmod :: chart_Series()下面绘制热图.如何将以下热图添加到chart_Series(或xts :: plot.xts):
library(quantmod)
# Get data fro symbol from Google Finance
symbol <- "SPY"
src <- "google"
from <- "2017-01-01"
symbolData <- getSymbols(symbol, src=src, from=from, auto.assign=FALSE)
# Calculate simple returns
symbolData.ret <- ROC(Cl(symbolData), type="discrete")
# Calculate lagged autocorrelations (Pearson correlation for each value of lag)
nLags <- 100
averageLength <- 3
symbolData.laggedAutocorr <- matrix(0, nLags, NROW(symbolData.ret))
for (lag in 2: nLags) {
# Set the average length as M
if (averageLength == 0) M <- lag
else …Run Code Online (Sandbox Code Playgroud)