I have a data object called NKLA
that looks like this:
> class(NKLA)
[1] "xts" "zoo"
> head(NKLA)
NKLA.Open NKLA.High NKLA.Low NKLA.Close NKLA.Volume NKLA.Adjusted
2018-06-11 9.57 9.58 9.57 9.58 402600 9.58
2018-06-12 9.56 9.56 9.56 9.56 300000 9.56
2018-06-13 9.57 9.58 9.56 9.57 179100 9.57
2018-06-14 9.57 9.57 9.57 9.57 0 9.57
2018-06-15 9.57 9.57 9.57 9.57 0 9.57
2018-06-18 9.54 9.58 9.54 9.58 300 9.58
But when I try to convert it to data.frame like
data.frame(date=index(NKLA), coredata(NKLA))
as.data.table(NKLA)
as.data.frame.matrix(NKLA)
as.data.frame.table(NKLA)
as.data.table(NKLA)
as.data.frame(NKLA)
I each time get an [1] "xts" "zoo"
to the query class(NKLA)
.
My question:
xts
/zoo
object into a data.frame
object?Any help appreciated.
System used:
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
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"))