Home > Blockchain >  i am getting double date and time column after using xts? and getting error 'x' must be a
i am getting double date and time column after using xts? and getting error 'x' must be a

Time:12-31

I am facing an issue. I am trying to convert my data into xts object so later I can merge time series and plot them. but I am getting an error 'x' must be a time-series object.I am going to show an example data here.

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)
value1<-as.numeric(value1)
value2<-c(1,21)
value2<-as.numeric(value2)
time_dataframe<-cbind.data.frame(time_p,value1,value2)
time_dataframe<-xts(time_dataframe,order.by(time_p)

but when I plot, I get an error 'x' must be a time-series.

and also I am getting in output 2-times date and time column as shown below.

                         time_p            value 1    value 2
2021-11-21 22:45:00  2021-11-21 22:45:00      1          1
2021-11-21 23:25:00  2021-11-21 23:25:00      9          21 

CodePudding user response:

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
  • Related