I have two zoo objects and I am wondering, why the first zoo object shows
..$ : NULL
when the second one shows
..$ : chr [1:2266] "1" "2" "3" "4" ...
instead.
What does this mean in this case? And how could I change
"..$ : chr [1:2266] "1" "2" "3" "4" ..." to "..$ : NULL" in the second object?
Background: I am conducting an eventstudy (package eventstudies) and I get the error:
Error in rval[i, j, drop = drop., ...] : subscript out of bounds
One of the possible problems could be the formatting of the data. The object "OtherReturns" is used as example data in the package. I am hoping to resolve the error by understanding more about zoo objects. Yet, I could not understand the formatting difference between the two following objects:
> str(OtherReturns)
‘zoo’ series from 2010-07-01 to 2013-03-28
Data: num [1:720, 1:4] -1.1568 -0.2727 -0.0229 1.01 -0.9107 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:4] "NiftyIndex" "CallMoneyRate" "SP500" "USDINR"
Index: Date[1:720], format: "2010-07-01" "2010-07-02" "2010-07-05" "2010-07-06" "2010-07-07" "2010-07-08" "2010-07-09" "2010-07-12" ...
> str(zoo_SP500)
‘zoo’ series from 2007-01-03 to 2015-12-31
Data: num [1:2266, 1:2] 1417 1418 1410 1413 1412 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:2266] "1" "2" "3" "4" ...
..$ : chr [1:2] "SP500_Closing" "Index_return"
Index: Date[1:2266], format: "2007-01-03" "2007-01-04" "2007-01-05" "2007-01-08" "2007-01-09" "2007-01-10" "2007-01-11" "2007-01-12" ...
At the first glance, both objects look the same in this regard:
> head(OtherReturns,3)
NiftyIndex CallMoneyRate SP500 USDINR
2010-07-01 -1.15678265 NA NA 0.3009785
2010-07-02 -0.27267977 NA -1.341017 0.0000000
2010-07-05 -0.02291607 NA NA 0.4070272
> head(zoo_SP500,3)
SP500_Closing Index_return
2007-01-03 1416.60 -0.1198618
2007-01-04 1418.34 0.1228293
2007-01-05 1409.71 -0.6084578
Any help and explanations much appreciated!
Thank you!
I have two data frames and I need to put the line of my second data frame as the last line of my first data frame:
The first data frame is PETR3.SA:
tail(PETR3.SA)
PETR3.SA.Open PETR3.SA.High PETR3.SA.Low PETR3.SA.Close
2020-04-23 17.35522 17.63133 16.85232 17.09884
2020-04-24 16.86218 17.01009 15.30415 15.84650
2020-04-27 16.14233 16.68468 15.74789 16.56635
2020-04-28 17.49000 18.02000 17.11000 18.02000
2020-04-29 18.51000 19.30000 18.35000 19.00000
2020-04-30 18.73000 19.18000 18.43000 18.65000
PETR3.SA.Volume PETR3.SA.Adjusted
2020-04-23 19498900 17.09884
2020-04-24 39716700 15.84650
2020-04-27 25446600 16.56635
2020-04-28 24004700 18.02000
2020-04-29 26938000 19.00000
2020-04-30 23209200 18.65000
str(PETR3.SA)
An ‘xts’ object on 2015-01-02/2020-04-30 containing:
Data: num [1:1322, 1:6] 9.07 8.18 7.84 7.86 8.15 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "PETR3.SA.Open" "PETR3.SA.High" "PETR3.SA.Low" "PETR3.SA.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 3
$ src : chr "yahoo"
$ updated : POSIXct[1:1], format: "2020-05-04 17:14:02"
$ na.action: 'omit' int [1:3] 779 1038 1281
..- attr(*, "index")= num [1:3] 1.52e+09 1.55e+09 1.58e+09
My second df:
cotacao_xts
PETR3.SA.Open PETR3.SA.High PETR3.SA.Low PETR3.SA.Close
2020-05-04 19.02 19.02 19.02 19.02
PETR3.SA.Volume PETR3.SA.Adjusted
2020-05-04 0 19.02
> str(cotacao_xts)
An ‘xts’ object on 2020-05-04/2020-05-04 containing:
Data: num [1, 1:6] 19 19 19 19 0 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "PETR3.SA.Open" "PETR3.SA.High" "PETR3.SA.Low" "PETR3.SA.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
NULL
I need to put my second df (cotacao_xts) as the last line of my first df.
I tried bind_rows, but this is what I got:
> new_df <- PETR3.SA %>%
+ bind_rows(cotacao_xts)
Error: Argument 1 must have names
As these are xts objects, we can use rbind assuming the index are unique
library(xts)
rbind(PETR3.SA, cotacao_xts)
methods(class = 'xts')[50]
#[1] "rbind.xts"
According to ?bind_rows
... - Data frames to combine
It can be data.table, data.frame or tbl_df. The xts object is neither one of those. It is a matrix with xts attribute. If we need to use bind_rows, then the objects needs to be converted to data.frame
So I have large list of xts objects which is OHLC data for 900 plus names. It orginally came in a dataframe coupled with other stuff I didn't need. I now want to use it in quantmod using getsymbols and load it into my environment however it is taking far too long. Anyone know a more efficient way of doing this or what I might be doing wrong? Can getSymbols handle a list of xts?
load(file = "biglistofdataframes.Rdata")
### Convert the list of dataframes to xts
list_of_xts <- lapply(listofdataframes,function(x) xts(x[,2:6],x$date))
####change column names to match quantmod
list_of_xts <- lapply(list_of_xts, setNames,nm = c("Open","High","Low","Close","Volume"))
####Save to Rdatafile
save(list_of_xts, file="1.RData")
#First I clear the environment then I load the data back into the environment
load("1.RData")
##
getSymbols("list_of_xts", src="RData", auto.assign=TRUE)#this craps out on me
The reason I am trying to get it into this format is so that I can replicate Ross Bennett's momentum code. See below
https://rbresearch.wordpress.com/2012/10/20/momentum-in-r-part-2/
I wouldn't expect this code to work:
getSymbols("list_of_xts", src = "RData", auto.assign = TRUE)
?getSymbols.RData says that Symbols (the first argument) should be "a character vector specifying the names of each symbol to be loaded". You don't have a symbol and file named "list_of_xts.RData".
Also, getSymbols.RData() expects each symbol to be in its own file, so you would have to write each xts object in your list to a separate file.
# Get some data
env_of_xts <- new.env()
getSymbols(symbols, env=env_of_xts)
# Write it to a temporary directory
tdir <- tempdir()
for (nm in names(env_of_xts)) {
save(list = nm, file = file.path(tdir, paste0(nm, ".RData")), envir = env_of_xts)
}
# Now you can use getSymbols() to load from file
getSymbols(symbols[1], src = "RData", dir = tdir, extension = "RData")
# [1] "AAPL"
head(AAPL)
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
# 2007-01-03 12.32714 12.36857 11.70000 11.97143 309579900 8.137179
# 2007-01-04 12.00714 12.27857 11.97429 12.23714 211815100 8.317789
# 2007-01-05 12.25286 12.31428 12.05714 12.15000 208685400 8.258555
# 2007-01-08 12.28000 12.36143 12.18286 12.21000 199276700 8.299341
# 2007-01-09 12.35000 13.28286 12.16429 13.22429 837324600 8.988768
# 2007-01-10 13.53571 13.97143 13.35000 13.85714 738220000 9.418928
i am unsure about what symbols RData contains, I do know however that you can generate a list of many xts with lapply and getsymbols. Here is my example using google for current stock data.
symbols<-c("AAPL", "AMZN","GOOGL", "F", "GM", "IBM", "ORCL")
List_of_xts<-lapply(symbols, function(sym){
List_of_Xts<-getSymbols(Symbols = sym, src = "google", auto.assign = FALSE)
})
str(List_of_xts)
resulting in:
> str(List_of_xts)
List of 7
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 1:5] 12.3 12 12.2 12.3 12.3 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:05"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 1:5] 38.7 38.6 38.7 38.2 37.6 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "AMZN.Open" "AMZN.High" "AMZN.Low" "AMZN.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:06"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 1:5] 233 235 241 244 243 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "GOOGL.Open" "GOOGL.High" "GOOGL.Low" "GOOGL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:07"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 7.56 7.56 7.72 7.63 7.75 7.79 7.73 7.77 7.89 7.97 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "F.Open" "F.High" "F.Low" "F.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:08"
$ :An ‘xts’ object on 2010-11-18/2018-02-07 containing:
Data: num [1:1817, 1:5] 35 34.1 34.2 34 33.7 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "GM.Open" "GM.High" "GM.Low" "GM.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:08"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 97.2 97.2 97.6 98.5 99.1 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "IBM.Open" "IBM.High" "IBM.Low" "IBM.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:09"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 17.2 17.6 17.6 17.6 17.9 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "ORCL.Open" "ORCL.High" "ORCL.Low" "ORCL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:10"> str(List_of_xts)
List of 7
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 1:5] 12.3 12 12.2 12.3 12.3 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:05"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 1:5] 38.7 38.6 38.7 38.2 37.6 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "AMZN.Open" "AMZN.High" "AMZN.Low" "AMZN.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:06"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2794, 1:5] 233 235 241 244 243 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "GOOGL.Open" "GOOGL.High" "GOOGL.Low" "GOOGL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:07"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 7.56 7.56 7.72 7.63 7.75 7.79 7.73 7.77 7.89 7.97 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "F.Open" "F.High" "F.Low" "F.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:08"
$ :An ‘xts’ object on 2010-11-18/2018-02-07 containing:
Data: num [1:1817, 1:5] 35 34.1 34.2 34 33.7 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "GM.Open" "GM.High" "GM.Low" "GM.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:08"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 97.2 97.2 97.6 98.5 99.1 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "IBM.Open" "IBM.High" "IBM.Low" "IBM.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:09"
$ :An ‘xts’ object on 2007-01-03/2018-02-07 containing:
Data: num [1:2795, 1:5] 17.2 17.6 17.6 17.6 17.9 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:5] "ORCL.Open" "ORCL.High" "ORCL.Low" "ORCL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "google"
..$ updated: POSIXct[1:1], format: "2018-02-07 21:48:10"
I can generate a list of timeDate objects for New York Exchange. However, most of the analytical functions expect a single timeDate object. The underlying data representation is POSIXct, so I can't just append them like a vector or a list.
How to do it?
library(timeDate)
x <- lapply(c(1885: 1886), holidayNYSE)
x
[[1]]
NewYork
[1] [1885-01-01] [1885-02-23] [1885-04-03] [1885-11-03] [1885-11-26] [1885-12-25]
[[2]]
NewYork
[1] [1886-01-01] [1886-02-22] [1886-04-23] [1886-05-31] [1886-07-05] [1886-11-02] [1886-11-25]
class(x[[1]])
[1] "timeDate"
attr(,"package")
[1] "timeDate"
class(x[[1]]#Data)
[1] "POSIXct" "POSIXt"
# ??? How to my two datetime objects ???
We can use do.call with c
x1 <- do.call(c, x)
x1
#NewYork
#[1] [1885-01-01] [1885-02-23] [1885-04-03] [1885-11-03] [1885-11-26] [1885-12-25] [1886-01-01] [1886-02-22] [1886-04-23] [1886-05-31] [1886-07-05] [1886-11-02]
#[13] [1886-11-25]
str(x1)
#Formal class 'timeDate' [package "timeDate"] with 3 slots
# ..# Data : POSIXct[1:13], format: "1885-01-01 05:00:00" "1885-02-23 05:00:00" "1885-04-03 05:00:00" "1885-11-03 05:00:00" ...
# ..# format : chr "%Y-%m-%d"
# ..# FinCenter: chr "NewYork"
and the structure of OP's list is
str(x)
#List of 2
#$ :Formal class 'timeDate' [package "timeDate"] with 3 slots
# .. ..# Data : POSIXct[1:6], format: "1885-01-01 05:00:00" "1885-02-23 05:00:00" "1885-04-03 05:00:00" "1885-11-03 05:00:00" ...
# .. ..# format : chr "%Y-%m-%d"
# .. ..# FinCenter: chr "NewYork"
# $ :Formal class 'timeDate' [package "timeDate"] with 3 slots
# .. ..# Data : POSIXct[1:7], format: "1886-01-01 05:00:00" "1886-02-22 05:00:00" "1886-04-23 05:00:00" "1886-05-31 05:00:00" ...
# .. ..# format : chr "%Y-%m-%d"
# .. ..# FinCenter: chr "NewYork"
I would like to subset dates from an xts object based on logical values in another xts object, but R returns an out-of-range error despite having in-range values.
For example I would like to filter dates and prices where RSI is above 60.
> strength <- RSI(d, 14)>60
> strength["2016-10-17::"]
RSI
2016-10-17 TRUE
2016-10-18 TRUE
2016-10-19 TRUE
2016-10-20 FALSE
2016-10-21 FALSE
> d["2016-10-17::"]
Open
2016-10-17 642.2760
2016-10-18 640.5988
2016-10-19 637.0000
2016-10-20 631.9800
2016-10-21 633.6470
> d["2016-10-17::"][strength == TRUE]
Error in `[.xts`(d["2016-10-17::"], strength == TRUE) :
'i' or 'j' out of range
This is not the output I expect because both my objects have data until 2016-10-21. What could be wrong? I would like something like :
> d["2016-10-17::"][strength == TRUE]
Open
2016-10-17 642.2760
2016-10-18 640.5988
2016-10-19 637.0000
This is the str of my xts objects :
> str(d)
An ‘xts’ object on 2013-09-02/2016-10-21 containing:
Data: num [1:1146, 1] 127 128 121 121 116 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "Open"
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
$ dateFormat: chr "Date"
$ na.action :Class 'omit' atomic [1:92] 1 2 3 4 5 6 7 8 9 10 ...
.. ..- attr(*, "index")= num [1:92] 1.37e+09 1.37e+09 1.37e+09 1.37e+09 1.37e+09 ...
> str(strength)
An ‘xts’ object on 2013-09-16/2016-10-21 containing:
Data: logi [1:1132, 1] FALSE FALSE FALSE FALSE FALSE FALSE ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "RSI"
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
NULL
>
Thank you
You didn't make a reproducible example, so here is some toy data. Your issue is that you did not subset strength with the same time window (so your inner strength == TRUE logical series has different row length to your d row length, generating your error. i.e. NROW(strength == TRUE) >> NROW(d["2016-10-17::"]) ):
library(quantmod)
getSymbols("AAPL")
d <- AAPL
strength <- RSI(Cl(d)) > 60
You shouldn't get an error if you do this:
d["2016-10-17::"][strength["2016-10-17::"] == TRUE]