How to Change Xts to Data.Frame and Keep Index

How can I change XTS to data.frame and keep Index?

That's because the dates are rownames in your data.frame. You need to make them a separate column.

Try this:

 data.frame(date=index(master_1), coredata(master_1))

Converting an xts object to a useful data frame

You can create your data.frame like this for example :

 data.frame(value=coredata(d3),timestamp=index(d3))
# value timestamp
# 1 65 2013-06-02 00:12:00
# 2 70 2013-06-02 00:15:00
# 3 70 2013-06-02 00:30:00
# 4 70 2013-06-02 00:45:00
# 5 70 2013-06-02 01:00:00

I advise you also to use read.zoo to read your data as a zoo object and avoid coercing xts by hand. For example:

dat <- read.zoo(text='timestamp,value
"2013-06-02 00:00:00",70
"2013-06-02 00:02:00",70
"2013-06-02 00:07:00",60
"2013-06-02 00:15:00",70
"2013-06-02 00:12:00",60
"2013-06-02 00:30:00",70
"2013-06-02 00:45:00",70
"2013-06-02 01:00:00",70',tz ='' , format = "%Y-%m-%d %H:%M:%S",header=TRUE,
sep=',')
d2 <- as.xts(dat)

Convert xts/zoo object to data.frame in R

We could use fortify.zoo

library(zoo)
df1 <- fortify.zoo(NKLA)

-output

> str(df1)
'data.frame': 6 obs. of 7 variables:
$ Index : POSIXct, format: "2018-06-11" "2018-06-12" "2018-06-13" "2018-06-14" ...
$ NKLA.Open : num 9.57 9.56 9.57 9.57 9.57 9.54
$ NKLA.High : num 9.58 9.56 9.58 9.57 9.57 9.58
$ NKLA.Low : num 9.57 9.56 9.56 9.57 9.57 9.54
$ NKLA.Close : num 9.58 9.56 9.57 9.57 9.57 9.58
$ NKLA.Volume : num 402600 300000 179100 0 0 ...
$ NKLA.Adjusted: num 9.58 9.56 9.57 9.57 9.57 9.58

data

NKLA <- structure(c(9.57, 9.56, 9.57, 9.57, 9.57, 9.54, 9.58, 9.56, 9.58, 
9.57, 9.57, 9.58, 9.57, 9.56, 9.56, 9.57, 9.57, 9.54, 9.58, 9.56,
9.57, 9.57, 9.57, 9.58, 402600, 3e+05, 179100, 0, 0, 300, 9.58,
9.56, 9.57, 9.57, 9.57, 9.58), .Dim = c(6L, 6L), .Dimnames = list(
NULL, c("NKLA.Open", "NKLA.High", "NKLA.Low", "NKLA.Close",
"NKLA.Volume", "NKLA.Adjusted")), index = structure(c(1528689600,
1528776000, 1528862400, 1528948800, 1529035200, 1529294400), tzone = "", tclass = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"))

Store XTS objects as data frames in a list in R

It is better to initialize a list with fixed length and name it with the tickers. In the OP's code, it is looping over the tickers directly, so each 'i' is the ticker name which is a string

dfList <- vector('list', length(tickers))
names(dfList) <- tickers

As the i here is a string name of the object "AAPL" or "AMZN", we can use get to return the value of that object from the global env

for (i in tickers) {
dfList[[i]] <- fortify.zoo(get(i)) %>%
select(c(1,5))
}

-check the dimensions

sapply(dfList, dim)
# AAPL AMZN
#[1,] 1507 1507
#[2,] 2 2

Another approach is mget to return all those objects into a list

library(purrr)
library(dplyr)
dfList2 <- mget(tickers) %>%
map(~ fortify.zoo(.x) %>%
select(1, 5))

Convert data.frame to xts object and preserve types

No, you can't. xts/zoo objects are a matrix with an index attribute and you can't mix types in a matrix.

We've considered creating an xts-data.frame class but a primary concern of xts is speed and memory efficiency. data.frames are not speed and memory efficient, so this hasn't been a priority.

Converting Multiple existing xts objects to multiple data.frames

By wrapping with which, it just returns the position with the names of the objects and not the actual objects. We may need to get the value of the objects with the names from that 'xtsObjects' output (mget - returns the values of multiple objects in a list, then loop over the list with map and convert to data.frame)

library(dplyr)
library(purrr)
out <- mget(names(xtsObjects)) %>%
map(fortify.zoo)

-output

> str(out)
List of 4
$ PDD :'data.frame': 793 obs. of 7 variables:
..$ Index : Date[1:793], format: "2018-07-26" "2018-07-27" "2018-07-30" "2018-07-31" ...
..$ PDD.Open : num [1:793] 26.5 27.5 23.3 22.2 19.4 ...
..$ PDD.High : num [1:793] 27.5 27.5 23.9 22.7 21.4 ...
..$ PDD.Low : num [1:793] 25 23.2 21.9 19.6 18.6 ...
..$ PDD.Close : num [1:793] 26.7 24.6 22.5 22.6 20.3 ...
..$ PDD.Volume : num [1:793] 43213200 19923300 13967700 13709600 19339000 ...
..$ PDD.Adjusted: num [1:793] 26.7 24.6 22.5 22.6 20.3 ...
$ RMO :'data.frame': 623 obs. of 7 variables:
..$ Index : Date[1:623], format: "2019-04-01" "2019-04-02" "2019-04-03" "2019-04-04" ...
..$ RMO.Open : num [1:623] 9.8 9.75 9.75 9.76 9.78 9.76 9.76 9.77 9.74 9.74 ...
..$ RMO.High : num [1:623] 9.8 9.75 9.75 9.76 9.78 9.76 9.76 9.77 9.74 9.74 ...
..$ RMO.Low : num [1:623] 9.75 9.75 9.75 9.76 9.75 9.76 9.76 9.77 9.74 9.74 ...
..$ RMO.Close : num [1:623] 9.75 9.75 9.75 9.76 9.75 9.76 9.76 9.77 9.74 9.74 ...
..$ RMO.Volume : num [1:623] 161000 11000 0 1000 100600 ...
..$ RMO.Adjusted: num [1:623] 9.75 9.75 9.75 9.76 9.75 9.76 9.76 9.77 9.74 9.74 ...
$ TSLA:'data.frame': 2826 obs. of 7 variables:
..$ Index : Date[1:2826], format: "2010-06-29" "2010-06-30" "2010-07-01" "2010-07-02" ...
..$ TSLA.Open : num [1:2826] 3.8 5.16 5 4.6 4 ...
..$ TSLA.High : num [1:2826] 5 6.08 5.18 4.62 4 ...
..$ TSLA.Low : num [1:2826] 3.51 4.66 4.05 3.74 3.17 ...
..$ TSLA.Close : num [1:2826] 4.78 4.77 4.39 3.84 3.22 ...
..$ TSLA.Volume : num [1:2826] 93831500 85935500 41094000 25699000 34334500 ...
..$ TSLA.Adjusted: num [1:2826] 4.78 4.77 4.39 3.84 3.22 ...
$ TSM :'data.frame': 3704 obs. of 7 variables:
..$ Index : Date[1:3704], format: "2007-01-03" "2007-01-04" "2007-01-05" "2007-01-08" ...
..$ TSM.Open : num [1:3704] 11.1 10.9 10.8 10.6 10.8 ...
..$ TSM.High : num [1:3704] 11.1 11 10.8 10.8 10.8 ...
..$ TSM.Low : num [1:3704] 10.8 10.7 10.5 10.6 10.5 ...
..$ TSM.Close : num [1:3704] 10.9 10.9 10.6 10.7 10.6 ...
..$ TSM.Volume : num [1:3704] 12438784 17678653 12691240 5477350 7396096 ...
..$ TSM.Adjusted: num [1:3704] 6.38 6.38 6.21 6.25 6.17 ...

Regarding the for loop doesn't do anything issue, 1) it is simply trying to do fortify.zoo on the object names and not on the values, 2) even if it is the value on which fortify.zoo is applied, it should be assigned back to the original object i.e. if we want to update the original object names, use assign

for (i in seq_along(xtsObjects)) {
assign(names(xtsObjects[i]), fortify.zoo(get(names(xtsObjects[i]))))
}


Related Topics



Leave a reply



Submit