-6

I have a file and I want to perform time series analysis on the data.Unfortunately I am unable to convert data into time series object?

Here is the snapshot of the code:

I have two column timestamp and closing price.I have tried various code but still unable to do so.

INTC <- read.csv("C:/Users/admin/Downloads/INTC-data.csv")[,c(2,6)]

head(INTC)

tail(INTC)

new_data<-xts(INTC)

Error:"Error in xts(INTC) : order.by requires an appropriate time-based object"

r2evans
  • 141,215
  • 6
  • 77
  • 149
cmpunk
  • 15
  • 3
  • 2
    Please take a look at how to make a [great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Posting screenshots of RStudio containing code is... well... not a great reproducible example. Especially take a look at how to show sample data. – Martin Gal Jul 14 '21 at 22:55
  • 1
    At this point: Perhaps the error *is* obvious: Since RStudio didn't find the function `as.xts.data.frame`, I'm asking myself if you loaded `library(xts)`. – Martin Gal Jul 14 '21 at 23:37
  • Good find, @MartinGal, that shows how much I notice in pics-of-code/data. Thanks for having the patience to find that. – r2evans Jul 14 '21 at 23:41
  • 1
    @r2evans Your comment made me take a closer look at the screenshot... I usally ignore those. But the question is actually about another error (which I noticed now...). Obviously I need to sleep. – Martin Gal Jul 14 '21 at 23:43
  • 2
    Read the information at the top of the [tag:r] tag. Note that it asks for easily reproducible test data, not images which require that those responding retype the data. – G. Grothendieck Jul 15 '21 at 00:09

1 Answers1

1

The question's use of the xts function is incorrect but it doesn't really matter since xts cannot represent that sort of data in the first place. xts requires an index class that represents dates or date/times such as Date or POSIXct but here we have plain numbers. Create a zoo series instead.

library(zoo)

# test data
INTC <- data.frame(ts = 930:935, close = 18.55)

z <- read.zoo(INTC); z
##   930   931   932   933   934   935 
## 18.55 18.55 18.55 18.55 18.55 18.55 
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thank you for your response. The ts column has time from 9.30 am to 4.00 pm for date between 19-June-06 to 30-June-06.How to convert ts(column) into time series object? – cmpunk Jul 15 '21 at 14:57
  • I cant be sure without more test data but perhaps: `library(xts); dates <- as.Date("2006-06-19") + cumsum(INTC$ts == 930) - 1; datetime <- as.POSIXct(sprintf("%s %04d", dates, INTC$ts), "%Y-%m-%d %H%M", tz = ""); xts(INTC$close, datetime)` – G. Grothendieck Jul 15 '21 at 17:25