2

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?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
jlee2222
  • 21
  • 2
  • Does this answer your question? [How to convert dataframe into time series?](https://stackoverflow.com/questions/29046311/how-to-convert-dataframe-into-time-series) – Paul Jul 22 '21 at 05:26
  • 1
    Hello. My concern is that my data doesn't have dates. It only has measurements with seconds. So I'm confused with that. It seems like time series requires year/month/date format but mine only has seconds. – jlee2222 Jul 22 '21 at 05:30
  • Hi :) thanks for your comment! Maybe the `lubridate` packages has usefull functions. I saw https://stackoverflow.com/a/62557879/10264278. Let us know if it helps a litte. – Paul Jul 22 '21 at 05:37
  • And here is a link towards official doc: https://lubridate.tidyverse.org/reference/lubridate-package.html#timespans – Paul Jul 22 '21 at 05:42
  • Maybe the `base::ts()` function is what you are looking for. But I do not know how to parameter it to your case. See ``?ts()`, https://stats.stackexchange.com/a/123010/227668 and https://robjhyndman.com/hyndsight/seasonal-periods/ that seems to explain the case of seconds. – Paul Jul 22 '21 at 06:05

2 Answers2

0

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    
')
jay.sf
  • 60,139
  • 8
  • 53
  • 110
0

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)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341