【问题标题】:How to create a time series object yyyy-mm-dd hh:mm:ss?如何创建时间序列对象 yyyy-mm-dd hh:mm:ss?
【发布时间】:2018-07-26 08:50:15
【问题描述】:

我正在尝试将声音数据转换为时间序列对象,以便进一步分析它(使用“bspec”包中的 f.ex. WelchPSD)。我的数据频率为 25811 个样本/秒 (Hz)。

我的声音文件 s2_d

> str(s2_d)
 int [1:52422753] -442 -434 -428 -413 -389 -386 -382 -387 -403 -373 ...

是我从 Waveread 在信号包中创建的 Wave 对象中提取数据后的整数。

文件开始和结束:

> begin <- as.POSIXct("2017-08-17 17:04:34", tz="UTC")

> end <- as.POSIXct("2017-08-17 17:38:25", tz="UTC")

我尝试使用以下几行将 s2_d 转换为 ts:

> s2_dts <- ts(s2_d, start = as.numeric(begin), end = as.numeric(end), frequency = 25811)

> s2_dts <- as.ts(s2_d, start = as.numeric(begin), end = as.numeric(end), frequency = 25811)

> s2_dts <- ts(s2_d, start = as.numeric(as.POSIXct(begin)), end = as.numeric(as.POSIXct(end)), frequency = 25811)

> s2_dts <- as.ts(s2_d, start = as.numeric(as.POSIXct(begin)), end = as.numeric(as.POSIXct(end)), frequency = 25811)

它们都将 s2_d 转换为 ts

> class(s2_dts)
[1] "ts"
> start(s2_dts)
[1] 1502989474          1

但是当我尝试函数 window() 时,我无法提取 f.ex。我的时间序列中有 1 秒 sn-p,所以有些地方不太对劲。

> x <- window(s2_dts, start = as.numeric(begin), end = as.numeric("2017-08-17 17:04:35"), extend= TRUE)
Error in if (start > end) stop("'start' cannot be after 'end'") : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In window.default(x, ...) : NAs introduced by coercion

我终于试过了:

> s2_dts <- ts(s2_d, start = c(2017,8,17,17,04,34), end = c(2017,8,17,17,38,25), frequency = 25811)

> start(s2_dts)
[1] 2017    8

> time(s2_dts)
Time Series:
Start = c(2017, 8) 
End = c(2017, 8) 
Frequency = 25811 [1] 2017

即使只识别年份和月份也会更好。如何获取包括日、小时、分钟和秒在内的整个时间作为时间序列的开始时间?

任何帮助将不胜感激。

【问题讨论】:

  • 注意ts 中的start 参数,它需要一个数字或两个整数的向量。使用start(s2_dts) 不会给出年份和月份,而是您在ts 中给出的前两个整数。仔细阅读开始参数的文档:“第一次观察的时间。单个数字或两个整数的向量,它们指定一个自然时间单位和一个(基于1的)样本数进入时间单位. 第二种形式的使用见例子。”
  • 并在window 中使用正确的end 参数以获得正确的输出:end = as.numeric(as.POSIXct("2017-08-17 17:04:35"))你忘记了as.POSIXct()。 @Outi
  • 感谢您的回复。我添加了 as.POSIXCT() 来结束,但仍然没有得到正确的答案。 >头(s2_dts)[1] -0.02425873 -0.15219589 -0.39131684 -0.42728653 0.25058982 1.64021640>头(x)的[1] 0.32049328 0.35900218 0.01488998 0.51875873 1.69127310 1.55796914>这两个头应该是相同的。跨度>
  • 我已经阅读了很多次 ts 的文档,但不明白我的情况如何给出开始和结束时间。正如您在我的回答中看到的那样,我已尝试将日期时间 Posixct 对象更改为文档中所述的单个数字。 @bVa 你能给个建议吗?

标签: r time-series


【解决方案1】:
data <- runif(10, 1, 10) # mock data
> data
[1] 9.455453 4.596560 9.675503 7.437548 2.722311 7.729820 5.737903 2.540364 3.512660 5.555681

# 2 samples/second 
begin <- as.POSIXct("2017-08-17 17:04:34", tz="UTC")
end <- as.POSIXct("2017-08-17 17:04:39", tz="UTC")

# Create time-series object
df_ts <- ts(data, 
            start = as.numeric(begin), 
            end = as.numeric(end), 
            frequency = 2)

> window(df_ts, 
         start = as.numeric(begin), 
         end = as.numeric(as.POSIXct("2017-08-17 17:04:36", tz="UTC")))
Time Series:
Start = c(1502989474, 1) 
End = c(1502989476, 1) 
Frequency = 2 
[1] 9.455453 4.596560 9.675503 7.437548 2.722311

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 2017-07-09
    • 2019-01-19
    • 1970-01-01
    • 2021-04-28
    • 2018-10-05
    相关资源
    最近更新 更多