% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  pdftitle={Data Analysis in RStudio},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[margin=1in]{geometry}
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering

\title{Data Analysis in RStudio}
\author{}
\date{\vspace{-2.5em}}

\begin{document}
\maketitle

\hypertarget{setup}{%
\section{Setup}\label{setup}}

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
  Install R.
  \href{https://www.digitalocean.com/community/tutorials/how-to-install-r-on-ubuntu-20-04}{Digital
  Ocean} has a nice guide on this (and many other things Linux-related,
  they are one of my preferred documentation sources)
\item
  Install RStudio. The
  \href{https://www.rstudio.com/products/rstudio/download/}{RStudio
  Downloads Page} has a Debian package you can download. Install with
  \texttt{sudo\ dpkg\ -i\ \textless{}pkgname\textgreater{}.deb}
\item
  Add data.table with Tools-\textgreater Install Packages\ldots, enter
  \texttt{data.table}
\item
  Install texlive full with \texttt{sudo\ apt\ install\ texlive-full}
\end{enumerate}

\hypertarget{datasets}{%
\section{Datasets}\label{datasets}}

Load profiles come from \url{http://wzy.ece.iastate.edu/Testsystem.html}

\hypertarget{analysis}{%
\section{Analysis}\label{analysis}}

\hypertarget{load-data}{%
\subsection{Load Data}\label{load-data}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{feeder_a <-}\StringTok{ }\KeywordTok{read.csv}\NormalTok{(}\StringTok{"feeder_a_data.csv"}\NormalTok{)}
\KeywordTok{names}\NormalTok{(feeder_a)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##  [1] "Time"          "Year"          "Month"         "Day.of.Week"  
##  [5] "Hour"          "Elapsed.Days"  "Elapsed.Hours" "Total.Power"  
##  [9] "Bus.1001"      "Bus.1002"      "Bus.1003"      "Bus.1004"     
## [13] "Bus.1005"      "Bus.1006"      "Bus.1007"      "Bus.1008"     
## [17] "Bus.1009"      "Bus.1010"      "Bus.1011"      "Bus.1012"     
## [21] "Bus.1013"      "Bus.1014"      "Bus.1015"      "Bus.1016"     
## [25] "Bus.1017"
\end{verbatim}

There's a lot of columns here from the different smart meters. Let's
just take the total power

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fa <-}\StringTok{ }\NormalTok{feeder_a[,}\KeywordTok{c}\NormalTok{(}\StringTok{"Month"}\NormalTok{,}\StringTok{"Day.of.Week"}\NormalTok{,}\StringTok{"Hour"}\NormalTok{,}\StringTok{"Elapsed.Days"}\NormalTok{,}\StringTok{"Total.Power"}\NormalTok{)]}
\end{Highlighting}
\end{Shaded}

Take a quick look at the loaded data. You can also use the
``Environment'' tab, but this won't show up in the notebook

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{head}\NormalTok{(fa)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   Month Day.of.Week Hour Elapsed.Days Total.Power
## 1     1           1    1         0.00       88.82
## 2     1           1    2         0.04       81.83
## 3     1           1    3         0.08       78.36
## 4     1           1    4         0.13       81.32
## 5     1           1    5         0.17       78.71
## 6     1           1    6         0.21       76.73
\end{verbatim}

\hypertarget{analyze-data}{%
\subsection{Analyze Data}\label{analyze-data}}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{hist}\NormalTok{(fa}\OperatorTok{$}\NormalTok{Total.Power)}
\end{Highlighting}
\end{Shaded}

\includegraphics{SmartMeterDataAnalysis_files/figure-latex/unnamed-chunk-4-1.pdf}
How to do arithmetic with data frame columns

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fa}\OperatorTok{$}\NormalTok{b12 <-}\StringTok{ }\NormalTok{feeder_a}\OperatorTok{$}\NormalTok{Bus}\FloatTok{.1001} \OperatorTok{+}\StringTok{ }\NormalTok{feeder_a}\OperatorTok{$}\NormalTok{Bus}\FloatTok{.1002}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{mean}\NormalTok{(fa}\OperatorTok{$}\NormalTok{Total.Power)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 105.8696
\end{verbatim}

There's a convenient command to print summary stastics for each column

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(fa)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##      Month         Day.of.Week         Hour        Elapsed.Days   
##  Min.   : 1.000   Min.   :1.000   Min.   : 0.00   Min.   :  0.00  
##  1st Qu.: 4.000   1st Qu.:2.000   1st Qu.: 5.75   1st Qu.: 91.24  
##  Median : 7.000   Median :4.000   Median :11.50   Median :182.48  
##  Mean   : 6.526   Mean   :3.992   Mean   :11.50   Mean   :182.48  
##  3rd Qu.:10.000   3rd Qu.:6.000   3rd Qu.:17.25   3rd Qu.:273.72  
##  Max.   :12.000   Max.   :7.000   Max.   :23.00   Max.   :364.96  
##                                                                   
##   Total.Power          b12   
##  Min.   : 42.68   Min.   :0  
##  1st Qu.: 68.07   1st Qu.:0  
##  Median : 94.11   Median :0  
##  Mean   :105.87   Mean   :0  
##  3rd Qu.:141.18   3rd Qu.:0  
##  Max.   :284.09   Max.   :0  
##                   NA's   :1
\end{verbatim}

Plot the power over time. Yikes! There's a lot of data here? Just how
long is it?

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(fa}\OperatorTok{$}\NormalTok{Total.Power,}\DataTypeTok{type=}\StringTok{"l"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{SmartMeterDataAnalysis_files/figure-latex/unnamed-chunk-8-1.pdf}
A whole year! That's too much!

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{max}\NormalTok{(fa}\OperatorTok{$}\NormalTok{Elapsed.Days)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 364.96
\end{verbatim}

We can plot just the first week by taking a subset of rows

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(fa[fa}\OperatorTok{$}\NormalTok{Elapsed.Days}\OperatorTok{<=}\DecValTok{7}\NormalTok{,}\StringTok{"Total.Power"}\NormalTok{],}\DataTypeTok{type=}\StringTok{"l"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\includegraphics{SmartMeterDataAnalysis_files/figure-latex/unnamed-chunk-10-1.pdf}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{names}\NormalTok{(fa)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] "Month"        "Day.of.Week"  "Hour"         "Elapsed.Days" "Total.Power" 
## [6] "b12"
\end{verbatim}

\hypertarget{how-to-replace-excel-pivot-tables-with-r-data-tables}{%
\subsection{How to Replace Excel Pivot Tables with R Data
Tables}\label{how-to-replace-excel-pivot-tables-with-r-data-tables}}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{require}\NormalTok{(data.table)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Loading required package: data.table
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ta <-}\StringTok{ }\KeywordTok{as.data.table}\NormalTok{(fa)}
\end{Highlighting}
\end{Shaded}

Let's look at how much power is used per day of week (1 = Sunday).
People are a little lazy on Mondays

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ta[,}\KeywordTok{mean}\NormalTok{(Total.Power),by=Day.of.Week]}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##    Day.of.Week        V1
## 1:           1  78.96709
## 2:           2 106.49592
## 3:           3 114.98106
## 4:           4 118.26026
## 5:           5 117.86812
## 6:           6 115.44796
## 7:           7  89.56215
\end{verbatim}

Now let's look at power vs month. It looks like air conditioning loads
cause a peak in July

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ta[,}\KeywordTok{mean}\NormalTok{(Total.Power),by=Month]}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##     Month        V1
##  1:     1 115.28683
##  2:     2 104.92824
##  3:     3 102.92249
##  4:     4  87.79007
##  5:     5  90.53629
##  6:     6 116.84221
##  7:     7 132.60573
##  8:     8 108.71017
##  9:     9 107.07769
## 10:    10  93.32519
## 11:    11  97.24442
## 12:    12 112.60633
\end{verbatim}

Finally, let's look at power vs time of day. The peak is at noon, though
in some regions/seasons it is common two have two peaks in
mornings/evenings

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{ta[,}\KeywordTok{mean}\NormalTok{(Total.Power),by=Hour]}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##     Hour        V1
##  1:    1  63.41608
##  2:    2  61.39458
##  3:    3  60.51874
##  4:    4  60.75890
##  5:    5  63.33058
##  6:    6  70.42581
##  7:    7  93.31123
##  8:    8 116.95967
##  9:    9 138.12444
## 10:   10 149.16318
## 11:   11 152.36315
## 12:   12 150.96052
## 13:   13 148.46293
## 14:   14 146.42252
## 15:   15 145.49814
## 16:   16 145.46781
## 17:   17 127.84674
## 18:   18 115.17044
## 19:   19 110.41701
## 20:   20 103.88964
## 21:   21  94.09581
## 22:   22  82.36200
## 23:   23  73.61830
## 24:    0  66.89233
##     Hour        V1
\end{verbatim}

\end{document}
