Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in plotting xts object: 'x' must be a time-series object

dataframe "Bangladesh" looked like-

  Province  Country      Cases  Date
1  NA       Bangladesh     0    2020-01-22
2  NA       Bangladesh     1    2020-01-23
3  NA       Bangladesh     2    2020-01-24
4  NA       Bangladesh     3    2020-01-25

To convert to xts i used the following code

Bangladesh_xts <- xts( Bangladesh, order.by= Bangladesh$Date)
Bangladesh_Final <- Bangladesh_xts[,-4]

           Province  Country      Cases
2020-01-22  NA       Bangladesh     0
2020-01-23  NA       Bangladesh     1
2020-01-24  NA       Bangladesh     2
2020-01-25  NA       Bangladesh     3

is.xts (Bangladesh_Final) shows "TRUE". But when I am trying

plot(Bangladesh_Final$Cases) This message is shown every time "Error in plot.xts(Bangladesh_Final$Cases) : 'x' must be a time-series object". What am i doing wrong?

like image 916
Afrida Shama Avatar asked Oct 31 '25 01:10

Afrida Shama


1 Answers

We need to select the Cases to avoid changing the class from numeric to character as xts is also a matrix and matrix can have only a single class

Bangladesh_xts <- xts( Bangladesh$Cases, order.by= as.Date(Bangladesh$Date))
like image 146
akrun Avatar answered Nov 01 '25 17:11

akrun