时间序列的 R 格式数据框(两个 ts 而不是一个?)
R Format data frame for Time Serie (two ts instead of one?)
挑战: 我已将 JSON 文档导入到数据框中,并希望将其绘制为单个时间序列。但是,我得到了两个系列。问题似乎出在我的格式上,但我一直未能找出问题所在。数据是每 5 秒测量一次的传感器数据。
所需的输出 是开始作为我的 X 线,值作为我的 Y 线。
Find the data here
脚本
#Clean work environment
rm(list = ls())
#Set options
setwd("C:/Users/Work/Directory")
url <- "device.json"
device <- fromJSON(url)
#Format date time
device$start <- strptime(device$start, "%Y-%m-%dT%H:%M:%OS")
#Create and plot ts
device <- ts(device, deltat = 0.05)
plot.ts(device)
最好使用xts
或zoo
对象来存储高频和不规则的时间序列数据。您可以像这样使用 zoo
包快速创建时间序列对象:
library(jsonlite)
library(zoo)
device <- fromJSON("device.json")
device$start <- strptime(device$start, "%Y-%m-%dT%H:%M:%OS")
device <- zoo(device$value, order.by = device$start)
plot(device)
挑战: 我已将 JSON 文档导入到数据框中,并希望将其绘制为单个时间序列。但是,我得到了两个系列。问题似乎出在我的格式上,但我一直未能找出问题所在。数据是每 5 秒测量一次的传感器数据。
所需的输出 是开始作为我的 X 线,值作为我的 Y 线。 Find the data here
脚本
#Clean work environment
rm(list = ls())
#Set options
setwd("C:/Users/Work/Directory")
url <- "device.json"
device <- fromJSON(url)
#Format date time
device$start <- strptime(device$start, "%Y-%m-%dT%H:%M:%OS")
#Create and plot ts
device <- ts(device, deltat = 0.05)
plot.ts(device)
最好使用xts
或zoo
对象来存储高频和不规则的时间序列数据。您可以像这样使用 zoo
包快速创建时间序列对象:
library(jsonlite)
library(zoo)
device <- fromJSON("device.json")
device$start <- strptime(device$start, "%Y-%m-%dT%H:%M:%OS")
device <- zoo(device$value, order.by = device$start)
plot(device)