diff --git a/Class6/Class_Examples/apply.r b/Class6/Class_Examples/apply.r new file mode 100644 index 0000000..99d2e7c --- /dev/null +++ b/Class6/Class_Examples/apply.r @@ -0,0 +1,8 @@ +DAT <- 1:8 +?apply +apply(DAT,mean) +?mapply +x <- as.data.frame(cbind(x1 = c(3,NA,12,7), x2 = c(4:1, 2:5)) ) +dimnames(x)[[1]] <- letters[1:8] +apply(x, 2, mean, trim = .2,na.rm=TRUE) +?mean \ No newline at end of file diff --git a/Class6/Slides/Function.png b/Class6/Slides/Function.png new file mode 100644 index 0000000..4baf7df Binary files /dev/null and b/Class6/Slides/Function.png differ diff --git a/Class6/Slides/If.png b/Class6/Slides/If.png new file mode 100644 index 0000000..c545516 Binary files /dev/null and b/Class6/Slides/If.png differ diff --git a/Class6/Slides/Loops_Tidy.tex b/Class6/Slides/Loops_Tidy.tex new file mode 100644 index 0000000..fc79cc5 --- /dev/null +++ b/Class6/Slides/Loops_Tidy.tex @@ -0,0 +1,111 @@ +\documentclass{beamer} +\usepackage{graphicx} +\usepackage{multicol} +\usepackage{hyperref} +\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} + \begin{enumerate} + \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<4->{apply functions } + \begin{itemize} + \onslide<5->{\item \texttt{while(a==b){Code to run}}} + \end{itemize} + \end{enumerate} +\end{frame} +\begin{frame}{Section Comments} + Section comments visually separate chunks of code. It is an excellent practice to distinguish types of analysis, or groups of code. + \newline +\vspace{1em} +\onslide<2->{\includegraphics[width=\textwidth]{Section_comments.png}} + \newline +\vspace{1em} +\onslide<3->{RStudio provides a keyboard shortcut to create these headers \texttt{(Cmd/Ctrl + Shift + R)}} +\end{frame} +%%%%%%%%%%%% +\begin{frame}{\emph{if} statements} + \only<1-8>{ + \emph{If} only run code when certain condtions are met + \newline + \onslide<2->{ \textbf{Possible uses}} + \begin{enumerate} + \onslide<3->{\item Load a file only if it does not exist } + \onslide<4->{\item Switch the value of an entry} + \onslide<5->{\item Create a new dummy variable} + \onslide<6->{\item Account for changes in column names on servers} + \end{enumerate} + \onslide<7->{Can be combined with \emph{else}} + \newline + \onslide<8->{The R function \texttt{ifelse()} can be a good shortcut} +} +\only<9>{\includegraphics[width=0.75\textwidth]{If.png}} +\end{frame} +%%%%%%%%%%%%%%%% +\begin{frame}{Class Exercise} +Use an if statement to write a csv file in a ``Data'' directory if the folder exists, otherwise it creates the directory first. +\end{frame} +%%%%%%%%%%%% +\begin{frame}{functions} + \only<1-6>{ + \emph{functions} save a set of code to use later. + \newline + \onslide<2->{They are exactly the same as the functions you already use in R} + \onslide<3->{Must define function inputs, code and output} + \begin{enumerate} + \onslide<4->{\item Use if you repeat code more than twice } + \onslide<4->{\item Stores complex code avoids typos} + \onslide<5->{\item Can be loaded in script files} + \onslide<6->{\item Could be used in your own library} + \end{enumerate} +} +\only<7>{\includegraphics[width=\textwidth]{Function.png}} +\only<8>{The \texttt{try()} function can be useful when creating your own functions. It prevents the code from stopping after a failure. Use with care.} +\end{frame} +%%%%%%%%%%%%%%%% +\begin{frame}{Class Exercise} + Turn the previous if statement into a function. Allow the user to pass in the name of the folder that is created. +\end{frame} +%%%%%%%%%%%%%%%%%%% +\begin{frame}{Class Exercise} + \only<1>{\LARGE Remember proper naming of files, data and scripts. Provide clear comments.} +\only<2>{ + \begin{enumerate} +\item Identify three data set you are interested in on FRED + \item Load one data set using a URL saved as a character variable + \item Create a raw data subdirectory and save the raw data into it + \item Clean the data, at least update the column names + \item Save the cleaned data as a CSV and an RDS file in a cleaned data subdirectory with proper names + \item Include a comment on the first line of the file explain what the code is used for and why. + \item Comment any other code as needed + \item Turn this code into a function where the user passes in the URL, and column names + \item Save function in a separate Rscript in a directory for scripts. Pay attention to the name + \item In the main directory create a Rscript and in that script load script + \item Use the function to clean all three data sets + \item After this add a section of code to find the mean, and summary of the data +\end{enumerate} +} +\end{frame} + +\end{document} diff --git a/Class6/Slides/Section_comments.png b/Class6/Slides/Section_comments.png new file mode 100644 index 0000000..2a31b06 Binary files /dev/null and b/Class6/Slides/Section_comments.png differ diff --git a/Class6/Slides/What_Comments.png b/Class6/Slides/What_Comments.png new file mode 100644 index 0000000..2ec77ea Binary files /dev/null and b/Class6/Slides/What_Comments.png differ diff --git a/Class6/Slides/Why_Comments.png b/Class6/Slides/Why_Comments.png new file mode 100644 index 0000000..3dd7f82 Binary files /dev/null and b/Class6/Slides/Why_Comments.png differ