How to convert seconds to time series?

Viewed 274

I have a data frame of measurement like this

Second      Current
0.000000    -0.007388           
0.336137    0.014361            
0.672274    0.001699            
1.008411    0.015404            
1.344548    0.009594    

How can I change these measurements into time series?

2 Answers

You could add an arbitrary date, e.g. the origin of UNIX epoch.

library(xts)
r <- xts(dat[-1], order.by=as.POSIXct('1970-01-01') + dat$Second)
#                       Current
# 1970-01-01 00:00:00 -0.007388
# 1970-01-01 00:00:00  0.014361
# 1970-01-01 00:00:00  0.001699
# 1970-01-01 00:00:01  0.015404
# 1970-01-01 00:00:01  0.009594

The times are actually still stored in decimal seconds:

strftime(time(r), '%OS7')
# [1] "00.000000" "00.336136" "00.672274" "01.008410" "01.344548"

Data:

dat <- read.table(header=T, text='Second      Current
0.000000    -0.007388           
0.336137    0.014361            
0.672274    0.001699            
1.008411    0.015404            
1.344548    0.009594    
')

1) zoo Assuming the input shown reproducibly in the Note at the end, we can convert the data frame to a zoo class series. We plot it giving the chart shown near the end.

library(zoo)
z <- read.zoo(DF); z
##         0    0.3361    0.6723    1.0084    1.3445 
## -0.007388  0.014361  0.001699  0.015404  0.009594 

plot(z)

2) ts

Since the times are regularly spaced another possibility is that we can convert it to a ts class series. z is from (1).

tt <- as.ts(z); tt
## Time Series:
## Start = 0 
## End = 1.344548 
## Frequency = 2.97497746454571 
## [1] -0.007388  0.014361  0.001699  0.015404  0.009594

2a) A variation on (2) is to round the frequency to 3 since it is close to that. tt is from (2).

tt3 <- ts(c(tt), start = start(tt), freq = round(frequency(tt))); tt3
## Time Series:
## Start = c(0, 1) 
## End = c(1, 2) 
## Frequency = 3 
## [1] -0.007388  0.014361  0.001699  0.015404  0.009594

screenshot

Note

Lines <- "Second Current
0.000000 -0.007388           
0.336137 0.014361            
0.672274 0.001699            
1.008411 0.015404            
1.344548 0.009594"
DF <- read.table(text = Lines, header = TRUE)
Related