Compare commits
No commits in common. "19680cca7ee3f4d59cdb3b13ea089ff8072c951c" and "78f247337f167257f4287a7999ac3cf5621ebaab" have entirely different histories.
19680cca7e
...
78f247337f
@ -5,7 +5,7 @@
|
|||||||
# so the results can reproduced more easily if the data changes.
|
# so the results can reproduced more easily if the data changes.
|
||||||
dir.create("Data",showWarnings = FALSE)
|
dir.create("Data",showWarnings = FALSE)
|
||||||
dir.create("Data/Orig_Data",showWarnings = FALSE)
|
dir.create("Data/Orig_Data",showWarnings = FALSE)
|
||||||
#Check if directory exits
|
#Check if direjctory exits
|
||||||
dir.exists("Data/Orig_Data")
|
dir.exists("Data/Orig_Data")
|
||||||
|
|
||||||
# Help --------------------------------------------------------------------
|
# Help --------------------------------------------------------------------
|
||||||
|
|||||||
@ -124,7 +124,7 @@
|
|||||||
\begin{frame}{File Management}
|
\begin{frame}{File Management}
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
\onslide<1->{\item Create directories for data}
|
\onslide<1->{\item Create directories for data}
|
||||||
\onslide<2->{\item Create directories for scripts}
|
\onslide<2->{\item Create Directories for scripts}
|
||||||
\onslide<3->{\item Make code do most of the cleaning}
|
\onslide<3->{\item Make code do most of the cleaning}
|
||||||
\onslide<4->{\item Clear naming of files}
|
\onslide<4->{\item Clear naming of files}
|
||||||
\onslide<5->{\item Document all files with a Readme file}
|
\onslide<5->{\item Document all files with a Readme file}
|
||||||
@ -145,20 +145,21 @@
|
|||||||
\column{0.5\textwidth}
|
\column{0.5\textwidth}
|
||||||
\textbf{Useful Functions}
|
\textbf{Useful Functions}
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
\onslide<2->{\item \texttt{dir.create()}}
|
\onslide<2->{\item \texttt{create.dir()}}
|
||||||
\onslide<3->{\item\texttt{dir.exists()}}
|
\onslide<3->{\item\texttt{dir.exists()}}
|
||||||
\onslide<4->{\item\texttt{file.exists()}}
|
\onslide<4->{\item\texttt{file.exists()}}
|
||||||
\onslide<5->{\item\texttt{list.files()}}
|
\onslide<5->{\item\texttt{list.files()}}
|
||||||
|
\onslide<6->{\item\texttt{dir.create()}}
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
\column{0.5\textwidth}
|
\column{0.5\textwidth}
|
||||||
\onslide<6->{\textbf{Saving Data}
|
\onslide<7->{\textbf{Saving Data}
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
\onslide<7->{\item \texttt{write\_csv()}}
|
\onslide<8->{\item \texttt{write\_csv()}}
|
||||||
\onslide<8->{\item \texttt{write\_excel\_csv()}}
|
\onslide<9->{\item \texttt{write\_excel\_csv()}}
|
||||||
\onslide<9->{\item\texttt{write\_delim()}}
|
\onslide<10->{\item\texttt{write\_delim()}}
|
||||||
\onslide<10->{\item\texttt{saveRDS()}}
|
\onslide<11->{\item\texttt{saveRDS()}}
|
||||||
\onslide<11->{\item\texttt{ggsave()}}
|
\onslide<12->{\item\texttt{ggsave()}}
|
||||||
\onslide<12->{\item\texttt{write.csv()}}
|
\onslide<13->{\item\texttt{write.csv()}}
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
}
|
}
|
||||||
\end{columns}
|
\end{columns}
|
||||||
@ -174,7 +175,7 @@
|
|||||||
Keep your main working space clean, and separate what you can.
|
Keep your main working space clean, and separate what you can.
|
||||||
\vspace{1.5em}
|
\vspace{1.5em}
|
||||||
\begin{enumerate}
|
\begin{enumerate}
|
||||||
\onslide<2->{\item Make different files for each main data step. Downloading, cleaning, various analyses.}
|
\onslide<2->{\item Make diffrent files for each main data step. Downloading, cleaning, various analyses.}
|
||||||
\onslide<3->{\item You can save the data downloading portion as a file called \texttt{1\_EIA\_Data\_Proc.R}}
|
\onslide<3->{\item You can save the data downloading portion as a file called \texttt{1\_EIA\_Data\_Proc.R}}
|
||||||
\onslide<4->{\item In the main file you could load the code with \texttt{source(``1\_EIA\_Data\_Proc.R'')}}
|
\onslide<4->{\item In the main file you could load the code with \texttt{source(``1\_EIA\_Data\_Proc.R'')}}
|
||||||
\onslide<5->{R will run the code in the current directory even if the script is saved elsewhere.}
|
\onslide<5->{R will run the code in the current directory even if the script is saved elsewhere.}
|
||||||
@ -217,7 +218,7 @@
|
|||||||
\onslide<3->{\item File names contain spaces}
|
\onslide<3->{\item File names contain spaces}
|
||||||
\onslide<4->{\item Same name but different capitalization}
|
\onslide<4->{\item Same name but different capitalization}
|
||||||
\onslide<5->{\item Names don’t describe their contents ``temp''}
|
\onslide<5->{\item Names don’t describe their contents ``temp''}
|
||||||
\onslide<6->{\item Outputs should be in separate directory from inputs}
|
\onslide<6->{\item Outputs should be in seperate directory from inputs}
|
||||||
\onslide<7->{\item Outputs files should have dates in the name}
|
\onslide<7->{\item Outputs files should have dates in the name}
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +0,0 @@
|
|||||||
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
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 51 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 43 KiB |
@ -1,111 +0,0 @@
|
|||||||
\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}
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 11 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 28 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 56 KiB |
Loading…
x
Reference in New Issue
Block a user