R obtaining rownames date using quantmod
You need to use the index
function. The xts
object isn't the same as a normal data.frame
, and has its own way of handling dimension names.
# Return all dates
index(GSPC)
Retreive row name from quantmod XTS file
I'm not familiar with R or quantmod but it looks to me like what you want to do is get the name of the row, not any column data, which is probably why your code is failing, hence it saying that it can't parse GDAXI[,0] as a Date object.
If you want to get a name of a row in quantmod, you need to use the index function, eg:
# Return all dates
index(GSPC)
You may also want to take a look at this related question.
How can I give dates as rownames to a data frame from quantmod::getSymbols?
The creators of anomalize have another package which you might find useful. It's called tidyquant and is perfect for what you are looking to do.
library(anomalize)
library(tidyquant)
library(tidyverse)
The Tidy Way to get Financial dataaapl <- tq_get("AAPL")
Referencing your end goal, we can chart AAPL's anomalies since 2008.aapl %>%
time_decompose(adjusted) %>%
anomalize(remainder) %>%
time_recompose() %>%
plot_anomalies(time_recomposed = TRUE, ncol = 3, alpha_dots = 0.5)
Change row names in R Studio
You can use the "tk_tbl" function from "timetk" package to convert the xts object to a data frame.
library(quantmod)
library(timetk)
mdate <- "2019-05-01"
edate <- "2019-05-08"
tickers <- c("MMM","C", "AAPL", "IBM", "AMZN")
rnames <- c("Open_1", "Open_2", "Open_3", "Open_4", "Open_D")
portfolioPrices <- NULL
for(ticker in tickers)
portfolioPrices <- cbind(portfolioPrices, getSymbols(ticker, from = mdate, to = edate, auto.assign = F)[,1])
#tk_tbl convert "xts" object to data frame, but the original dates will be transformed into a new column named "index"
timetk::tk_tbl(portfolioPrices)
# A tibble: 5 x 6
index MMM.Open C.Open AAPL.Open IBM.Open AMZN.Open
<date> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2019-05-01 189. 70.7 210. 141. 1933.
2 2019-05-02 184. 69.7 210. 140. 1913.
3 2019-05-03 186. 70.4 211. 140. 1949
4 2019-05-06 182. 69.1 204. 138. 1918.
5 2019-05-07 182. 69.3 206. 139. 1940.
#remove the "index" column and reassign colnames and rownames
portfolioPrices <- timetk::tk_tbl(portfolioPrices)[, -1]
colnames(portfolioPrices) = tickers
rownames(portfolioPrices) = rnames
R yearly returns from dataframe with date and close
Your code almost works if you just convert to xts
. If you're having difficulty converting your data.frame
to xts
, then provide more info about your data as requested in the comments of your question.
getSymbols("SPY", src='yahoo', return.class='data.frame')
#[1] "SPY"
class(SPY)
#[1] "data.frame"
as.data.frame(periodReturn(xts(SPY[["SPY.Close"]], as.Date(rownames(SPY))),
'yearly', subset="2008/"))
yearly.returns
2008-12-31 -0.382805554
2009-12-31 0.234929078
2010-12-31 0.128409907
2011-12-30 -0.001988072
2012-06-04 0.020717131
R: Export CSV with dates from getSymbols(), quantmod package
So when you import the data for AAPL it has dates in rownames thats why when you try to export it, it doesnt include the date.
USE THIS
getSymbols("AAPL", src = "yahoo")
After importing use the code below and then export x:
x<-data.frame(AAPL)
x$date<-rownames(x)
rownames(x)<-NULL
How to get min and max values with the date of accurance using quantmod
I can't tell if you date column is the rownames or an actual column, but the code below should produce your result if the date column is called date
in the dataframe.
do.call(rbind, apply(d[,-1], 2, function(col) {
max_ind <- which.max(col)
min_ind <- which.min(col)
list(max=col[max_ind], max_date=d$date[max_ind], min=col[min_ind],
min_date=d$date[min_ind])
}))
and if the date column is the rownames
do.call(rbind, apply(d, 2, function(col) {
max_ind <- which.max(col)
min_ind <- which.min(col)
list(max=col[max_ind], max_date=as.character(index(d))[max_ind], min=col[min_ind],
min_date=as.character(index(d))[min_ind])
}))
The code does an apply over columns finding the index of the maximum and minimum, then returns the values and dates corresponding to these.
how to extract row index from data.frame in R with quantmod package
It is not a data.frame, it is an xts. If you want to manipulate it as a data.frame and get the dates (they come over as rownames) try:
df <- data.frame(F)
row.names(df)
Obtain date column from xts object
getSymbols
does not return a data.frame
by default; it returns an xts object. xts objects do not have row names. They have an index attribute that you can access with the index
function.
Related Topics
Subset a Data.Frame with Multiple Conditions
Difference of Prediction Results in Random Forest Model
Findassocs for Multiple Terms in R
Match Dataframes Excluding Last Non-Na Value and Disregarding Order
Adding Percentages to a Grouped Barchart Columns in Ggplot2
Problems with Dplyr and Posixlt Data
Renaming Multiple Columns with Dplyr Rename(Across(
Compute All Pairwise Differences Within a Vector in R
How to Rename Element's List Indexed by a Loop in R
Warning: Replacing Previous Import 'Head' When Loading 'Utils' in R
Drawing Non-Intersecting Circles
Filled and Hollow Shapes Where the Fill Color = the Line Color
How to Calculate the Distance Between Latitude and Longitude Along Rows of Columns in R
R Corpus Is Messing Up My Utf-8 Encoded Text
How to Reverse Legend (Labels and Color) So High Value Starts at Bottom
Load a Dataset into R with Data() Using a Variable Instead of the Dataset Name