\documentclass{beamer} \usepackage{graphicx} \usepackage{multicol} \usepackage{hyperref} \usepackage{verbatim} \usepackage{lipsum} % for placeholder text \graphicspath{{pdf_images/}} \title{ECON 4530/5530 \\ Computational Economics} \subtitle{Data transformation and Loops} \author{Alex Gebben} \begin{document} % Title Slide \begin{frame} \titlepage \end{frame} %%%%%%%%%%%%%%%%%% \begin{frame}{Loops} \only<1->{There are three types of loops in R } \begin{enumerate} \onslide<2->{ \item{A \emph{for} loop runs for each value in a list}} \begin{itemize} \onslide<3->{\item \texttt{for(i in 1:10)\{Code to run\}}} \end{itemize} \onslide<4->{ \item{A \emph{While} loop runs if a condtion is metif a condtion is met.\emph{Can lead to infinite loops}}} \begin{itemize} \onslide<5->{\item \texttt{while(a==b)\{Code to run\}}} \end{itemize} \onslide<6->{\item apply functions } \begin{itemize} \onslide<7->{\item \texttt{while(a==b)\{Code to run\}}} \end{itemize} \end{enumerate} \end{frame} \begin{frame}{Loop examples} \only<1>{\includegraphics[width=\textwidth]{For_Loops.png}} \only<2>{\includegraphics[width=\textwidth]{While_Loops.png}} \only<3>{\includegraphics[width=0.5\textwidth]{While_Loops_Inf.png}} \only<4>{\includegraphics[width=\textwidth]{apply_functions.png}} \only<5>{\includegraphics[width=\textwidth]{sapply_loop.png}} \end{frame} \begin{frame}{Class Exercise} \begin{itemize} \item Create a loop that downloads a set of files when given a list of URLs. \item Name each file based on another list. \item Repeat for each type of loop (for, while, apply) \end{itemize} \end{frame} \begin{frame}{Data transformation} \begin{itemize} \item Data rarely comes in the form you need. \item Transformation helps prepare data for analysis and visualization. \item We'll use the \texttt{dplyr} package from the tidyverse. \end{itemize} \end{frame} % Slide 2 \begin{frame}{Core dplyr Verbs} \begin{itemize} \item \texttt{filter()} – select rows based on conditions \item \texttt{arrange()} – reorder rows \item \texttt{select()} – choose columns \item \texttt{mutate()} – add new columns \item \texttt{summarize()} – reduce multiple values to one \item \texttt{group\_by()} – group data for summary \end{itemize} \end{frame} % Slide 3 \begin{frame}{Using the Pipe Operator} \begin{itemize} \item Pipe: \texttt{|>} or \texttt{\%>\%} passes output to next function \end{itemize} \end{frame} % Slide 4 \begin{frame}{Working with Rows} \begin{itemize} \item \texttt{filter()} – keep rows meeting conditions \item \texttt{arrange()} – sort rows \item \texttt{distinct()} – remove duplicates \end{itemize} \end{frame} % Slide 5 \begin{frame}{Working with Columns} \begin{itemize} \item \texttt{select()} – choose columns \item \texttt{rename()} – rename columns \item \texttt{mutate()} – create new columns \end{itemize} \end{frame} % Slide 6 \begin{frame}{Grouped Operations} \begin{itemize} \item \texttt{group\_by()} – group data \item \texttt{summarize()} – compute summaries per group \item Useful for aggregation and comparisons \end{itemize} \end{frame} \begin{frame}[plain] \only<1>{\includegraphics[width=\textwidth]{Pipe_Example.png}} \only<2>{\includegraphics[width=0.8\textwidth]{Filter_Example.png}} \only<3>{\includegraphics[width=\textwidth]{group_and Summarize.png}} \end{frame} % Slide 8 \begin{frame}{Class Exercise} \textbf{Example:} dataset to apply dplyr: \texttt{airquality} \begin{itemize} \item Convert to a tibble \item Remove any entries with NA values \item Remove wind speed outliers \item Calculate the average temperature in each month \item Find the number of observations in each month \item Find the max, min and standard deviation of Wind speed \item Use pipes to complete all tasks in one line \end{itemize} \end{frame} \end{document}