Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Transform Data frame to Time Series [duplicate]

Tags:

r

time-series

I have a Google stock data. It has two columns Date(Daily Data) and Close i.e. Google closing index.

Date    Close
10/11/2013  871.99
10/10/2013   868.24
10/9/2013    855.86
10/8/2013   853.67
10/7/2013   865.74
10/4/2013   872.35
10/3/2013   876.09
10/2/2013   887.99
10/1/2013   887
9/30/2013   875.91
9/27/2013   876.39
9/26/2013   878.17
9/25/2013   877.23
9/24/2013   886.84

and its in csv format and I read it through read.csv which return data frame object. When I tried to transform it into timeseries / ts() object, it returns unwanted numbers.

Please help me to convert data frame into ts() object.

Thanks in advance.

like image 720
Ajay Avatar asked Nov 07 '13 12:11

Ajay


People also ask

How do I convert a Dataframe to time series data in R?

To convert the given dataframe with the date column to the time series object, the user first needs to import and load the xts package. The user then needs to call the xts() function with the required parameters the main need to call this function is to create the time-series object in R language and at the end use is.

How do I convert a data frame in R?

as. data. frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors.

How do I import time series data into R?

The easiest way import data in . csv files into R is to use the R function read. csv(). Now do the same for the Microsoft data.

How do you convert data to time series in python?

Convert data from a string to a timestamp: if we have a list of string data that resembles DateTime, we can first convert it to a dataframe using pd. DataFrame() method and convert it to DateTime column using pd. to_datetime() method.


2 Answers

I suggest using xts instead of ts as it has lot of functions especially for financial time series. If your data is in data.frame DF then you can convert it to xts as follows

xts(DF$Close, as.Date(DF$Date, format='%m/%d/%Y')
like image 105
CHP Avatar answered Sep 21 '22 16:09

CHP


Here's an approach using zoo from zoo package and then coercing the result to be ts

> library(zoo)
> ZOO <- zoo(df$Close, order.by=as.Date(as.character(df$Date), format='%m/%d/%Y'))
> ZOO
2013-09-24 2013-09-25 2013-09-26 2013-09-27 2013-09-30 2013-10-01 2013-10-02 2013-10-03 2013-10-04 
    886.84     877.23     878.17     876.39     875.91     887.00     887.99     876.09     872.35 
2013-10-07 2013-10-08 2013-10-09 2013-10-10 2013-10-11 
    865.74     853.67     855.86     868.24     871.99 
> ts(ZOO)  # coercing to be `ts`
Time Series:
Start = 1 
End = 14 
Frequency = 1 
 [1] 886.84 877.23 878.17 876.39 875.91 887.00 887.99 876.09 872.35 865.74 853.67 855.86 868.24
[14] 871.99
attr(,"index")
 [1] "2013-09-24" "2013-09-25" "2013-09-26" "2013-09-27" "2013-09-30" "2013-10-01" "2013-10-02"
 [8] "2013-10-03" "2013-10-04" "2013-10-07" "2013-10-08" "2013-10-09" "2013-10-10" "2013-10-11"
like image 20
Jilber Urbina Avatar answered Sep 20 '22 16:09

Jilber Urbina