S. Pal
S. Pal

Reputation: 21

Convert data into time series data using R

I have one csv file with two columns - one of monthly date & another sale in dollar. The table is:

The table head:

month   US dollar
31-12-1978  207.8
31-01-1979  227.3
28-02-1979  245.7
30-03-1979  242.1
30-04-1979  239.2
31-05-1979  257.6

The table bottom:

31-12-2013  1,225.40
31-01-2014  1,244.80
28-02-2014  1,301.00
31-03-2014  1,336.10
30-04-2014  1,299.00
30-05-2014  1,287.50

I am able to import the data correctly. But I am unable to convert it to time series data. The code I used:

data <- ts(data[,2],start = c(1978,12,31), end = c(2014,5,30), frequency = 12)

Thanks in advance.

Upvotes: 2

Views: 3892

Answers (1)

shiny
shiny

Reputation: 3502

What about this?

DATA

df <- read.table(text =c("
month         USdollar
31/12/1978  207.8
31/01/1979  227.3
28/02/1979  245.7
30/03/1979  242.1
30/04/1979  239.2
31/05/1979  257.6
31/12/2013  1,225.40
31/01/2014  1,244.80
28/02/2014  1,301.00
31/03/2014  1,336.10
30/04/2014  1,299.00
30/05/2014  1,287.50"),header = T)

Convert to time series

df1 <- xts(df$USdollar, as.Date(df$month, format = "%d/%m/%Y"))

OR

library(zoo)
df2 <- ts(zoo(df$USdollar, order.by=as.Date(as.character(df$month), format="%d/%m/%Y")))

Upvotes: 3

Related Questions