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.
Add Date Column to XTS Object
For turning a xts object into a dataframe with the date column you can use the following code. You use index to get the date index of the xts object and coredata for all the data contained in the xts object.
# my_xts is based on data from OP
df1 <- data.frame(Date = index(my_xts), coredata(my_xts) )
# show resulting structure
str(df1)
'data.frame': 12 obs. of 2 variables:
$ Date : Date, format: "2016-01-01" "2016-02-01" "2016-03-01" "2016-04-01" ...
$ AAPL.Returns: num -0.07524 -0.00668 0.12721 -0.13992 0.06529 ...
# outcome
df1
Date AAPL.Returns
1 2016-01-01 -0.075242313
2 2016-02-01 -0.006677563
3 2016-03-01 0.127210629
4 2016-04-01 -0.139921096
5 2016-05-01 0.065286997
6 2016-06-01 -0.042659753
7 2016-07-01 0.090062774
8 2016-08-01 0.018136446
9 2016-09-01 0.065504290
10 2016-10-01 0.004334348
11 2016-11-01 -0.026598591
12 2016-12-01 0.047955150
How can I extract the timestamp of an xts object in R?
Use index
(xtsobject)
data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')
index(sample.xts)
> index(sample.xts)
[1] "2007-01-02 EST" "2007-01-03 EST" "2007-01-04 EST" "2007-01-05 EST" "2007-01-06 EST" "2007-01-07 EST"
[7] "2007-01-08 EST" "2007-01-09 EST" "2007-01-10 EST" "2007-01-11 EST" "2007-01-12 EST" "2007-01-13 EST"
[13] "2007-01-14 EST" "2007-01-15 EST" "2007-01-16 EST" "2007-01-17 EST" "2007-01-18 EST" "2007-01-19 EST"
[19] "2007-01-20 EST" "2007-01-21 EST" "2007-01-22 EST" "2007-01-23 EST" "2007-01-24 EST" "2007-01-25 EST"
Extracting Dates from xts object based on vaule
You could try
index(A[diff(A)<0])
#[1] "2014-12-31" "2015-01-04"
index(A[diff(A)==1])
#[1] "2014-12-29" "2015-01-02"
i am getting double date and time column after using xts? and getting error 'x' must be a time-series object
The data part should not include the times.
library(xts)
time <- c("21.11.2021 22:45", "21.11.2021 23:25")
time_p <- as.POSIXct(time, format='%d.%m.%Y %H:%M')
value1 <- c(1, 9)
value2 <- c(1, 21)
xts(cbind(value1, value2), time_p)
## value1 value2
## 2021-11-21 22:45:00 1 1
## 2021-11-21 23:25:00 9 21
or
DF <- data.frame(time_p, value1, value2)
z <- read.zoo(DF)
as.xts(z)
# same
Extract week and day from an xts object into a column
You could use .indexwday
to get the weekday of the index. It returns the wday
vector from a POSIXlt
representation of the index, so see the Details section of ?POSIXlt
for a description.
require(quantmod)
getSymbols("SPY")
SPY$weekday <- .indexwday(SPY)
Using date and seconds from midnight columns to convert a data.frame to an xts object (R)
We can convert the 'DATE', 'TIME' column to Datetime class and convert the dataset to xts
by specifying the order.by
library(xts)
library(lubridate)
xts(df1[-(1:2)], order.by = as.POSIXct(paste(df1$DATE,
hms::hms(seconds_to_period(df1$TIME)))))
# Col1 Col2
#1993-01-04 09:35:38 10.250 10.000
#1994-01-05 09:35:41 10.250 10.111
#1997-03-16 09:35:46 10.250 10.222
#2001-08-28 09:36:05 10.251 10.444
#2006-04-20 09:40:07 10.251 10.555
#2017-11-10 09:36:01 10.251 10.333
NOTE: The index of xts
needs a Datetime class object and not a formatted character class vector
data
df1 <- structure(list(DATE = c("1993-01-04", "1994-01-05", "1997-03-16",
"2017-11-10", "2001-08-28", "2006-04-20"), TIME = c(34538L, 34541L,
34546L, 34561L, 34565L, 34807L), Col1 = c(10.25, 10.25, 10.25,
10.251, 10.251, 10.251), Col2 = c(10, 10.111, 10.222, 10.333,
10.444, 10.555)), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6"))
How to divide the columns of an xts object by the columns of another xts object by its column name in R
The trick is to get the data in the same order by sorting the column names and use this to select the columns in order. This only works if all the column names are the same in both xts objects.
Note: I have renamed the df1 and df2 objects to df1_xts and df2_xts to show that we are dealing with xts objects and not data.frames to avoid any confusion.
# select the column names of both xts objects, via sort
# and use matrix calculations to do the rest.
df1_xts[,sort(names(df1_xts))] / df2_xts[,sort(names(df2_xts))]
Argentina Brasil Chile Colombia Peru
2017-01-01 4 16 1 8 1
2018-01-01 2 2 2 2 9
2019-01-01 4 4 14 1 2
2020-01-01 3 2 12 3 2
2021-01-01 2 1 6 2 2
Related Topics
R Bookdown - Custom Title Page
Using Leaflet-Side-By-Side Plugin in R
Error: C Stack Usage Is Too Close to The Limit in R
How to Create Dynamic Number of Observeevent in Shiny
Linear Regression with Constraints on The Coefficients
Plot Histogram with Points Instead of Bars
Fill in Gaps (E.G. Not Single Cells) of Na Values in Raster Using a Neighborhood Analysis
Horizontal Rule in R Markdown/Bookdown Causing Errors
How to Fix Axis Margin with Ggplot2
Error with Scale_X_Labels in Ggplot2
R Eps Export and Import into Word 2010
Download Multiple CSV Files with One Button (Downloadhandler) with R Shiny
Change Font Size for All Inline Equations R Markdown
Get Country (And Continent) from Longitude and Latitude Point in R
Specific Spaces Between Bars in a Barplot - Ggplot2 - R