Search code examples
rdataframestrptime

Having trouble understanding the resulting format of strptime after converting from dataframe to time series (ts)


When I use strptime function in a dataframe the format of the date looks correct. However, when I then convert to ts (time series) so I can use functions like forecast, the data column looks like gibberish and often hard to see if it is correct or not. Consider the following code....

qtr <- c("03/01/2000","06/01/2000","09/01/2000","12/01/2000","03/01/2001","06/01/2001","09/01/2001","12/01/2001")
qtr <- strptime(as.character(qtr), format = "%m/%d/%Y",tz="EST")
dem <- c(1342,1382,1296, 1330, 1360, 1441,1343,1426)

# create data frame
data <- data.frame(qtr,dem)
data

The output from when I type data gives the following:

#          qtr  dem
# 1 2000-03-01 1342
# 2 2000-06-01 1382
# 3 2000-09-01 1296
# 4 2000-12-01 1330

As expected. However, when I then convert to a time series as below, i get an extra column.

ts=ts(data, start=c(2000,1), end= c(2001,4),frequency=4)
ts

output:

#          qtr        dem
# 2000 Q1  951886800 1342
# 2000 Q2  959835600 1382
# 2000 Q3  967784400 1296
# 2000 Q4  975646800 1330

Also, if I do a view (i.e., View(ts)) I can see only the second and 3rd columns and its difficult to figure out what the dates mean. What is the format of the middle column? What do the digits in say 975646800 represent?


Solution

  • If you specify data$dem in the ts-object the result should be as you expect.

    qtr <- c("03/01/2000","06/01/2000","09/01/2000","12/01/2000","03/01/2001","06/01/2001","09/01/2001","12/01/2001")
    qtr <- strptime(as.character(qtr), format = "%m/%d/%Y",tz="EST")
    dem <- c(1342,1382,1296, 1330, 1360, 1441,1343,1426)
    
    # create data frame
    data <- data.frame(qtr,dem)
    
    
    myts <- ts(data$dem, start=c(2000, 3), end=c(2001, 12), frequency=4)
    
    myts
    #>      Qtr1 Qtr2 Qtr3 Qtr4
    #> 2000           1342 1382
    #> 2001 1296 1330 1360 1441
    #> 2002 1343 1426 1342 1382
    #> 2003 1296 1330 1360 1441
    

    Created on 2020-01-29 by the reprex package (v0.3.0)