Quantcast
Channel: r software hub
Viewing all articles
Browse latest Browse all 1015

An Introduction to Time Series with JSON Data

$
0
0

By Divya Parmar

Revenue seasonal jpeg

For this post, I wanted to take the data analysis process in a different direction. Normally, an R analysis starts with data from a comma-separated Excel file (.csv) or a tab-separated file (.txt).

However, online data is often formatted in JSON, which stands for JavaScript Online Notation. JSON has different forms, but for this data, it consists of nested arrays in two main parts. One part is the meta-data header, and the other is the observations themselves. You can see that by looking at the file online here.

This data is LA city revenue, broken down by fiscal month and department. I want to get the total monthly revenue for the entire city and looks for seasonal trends.

To do this, we need to load in our JSON file, manipulate it, and apply time series analysis. Let’s get started.

JSON Manipulation

We load in the JSON file just as we would any other file type. However, R cannot read it, and that’s where “rjson” package comes into play. Once we extract the JSON file, we obtain the observations in the data section and create a variable for the number of observations.

#Install package
install.packages("rjson")
library(rjson)
jsonfile

As I mentioned, let's looks at the observations data, which is in arrays but can be manipulated like a matrix. We first create arrays to store the values, then loop over the arrays to get each value by its index. In JSON, we first get to the correct observation with the [[x]] parameter, then choose the value within the vector with the [9] parameter. We now have arrays for the fiscal year, month, department, revenue, and fiscal period.

datalength 

Data Frame Manipulation

First we need to combine our vectors into a data frame to combine monthly revenue.

#Bind columns and convert it to dataframe
revdata ...read more

Source:: r-bloggers.com


Viewing all articles
Browse latest Browse all 1015

Trending Articles