Philip
Philip

Reputation: 974

R Format data frame for Time Serie (two ts instead of one?)

The challenge: I’ve imported a JSON document into a data frame, and want to plot this as a single time serie. However, I’m getting two series. The problem seems to be with my formatting, but I have been unsuccessful in figuring out what the problem is. Data is sensor data measured every 5 second.

The desired output is to have start as my X line and value as my Y line. Find the data here

The script

#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)

Plot of device - should only be one time series, not two.

Upvotes: 0

Views: 77

Answers (1)

dickoa
dickoa

Reputation: 18437

It's better to use xts or zoo object to store high frequency and irregular time series data. You can quickly create your time series object using the zoo package like this:

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)

enter image description here

Upvotes: 1

Related Questions