1 General Rules and Principles
1.1 Installing R and RStudio
Here are the instructions for installing R and RStudio on your Windows or Mac desktop. Skip the third part and do not install “SDSFoundations Package.”
1.2 Basic rules and best practices
All files should exist in a local folder that syncs to a cloud-storage service. No file you ever work on should be at risk of being lost if your computer ceases to function or be in your possesion. NEVER place any file on “downloads” or “desktop” folders.
Get a free cloud-storage service with a desktop application that syncs to a cloud-storage service. I like the Dropbox desktop app but feel free to choose any other service. You don’t need a lot of space so free version of any desktop cloud app would work. Under the Dropbox folder, create a designated folder for this course such as EC282.
All subfolders under EC282 and files in them should have unique and descriptive construction: DON’T use spaces in file or folder names. Here is an example of a folder structure that might work for a student in this class:
EC282
│
│
└───Course_docs
│ │ SyllabusEC282.pdf
│ │ LectureNotes.pdf
└───Assignments
│ └───Assignment1
│ │ dataset1name.Rda
│ │ Lastname_Firstname_Assignment1_EC282.R
│ └───Assignment2
│ │ dataset2name.Rda
│ │ Lastname_Firstname_Assignment1_EC282.R
│ └───Assignment3
│ │ dataset3name.Rda
│ │ Lastname_Firstname_Assignment3_EC282.R
│ └───Assignment4
│ │ dataset4name.Rda
│ │ Lastname_Firstname_Assignment4_EC282.R
│ │ ...
└───Exams
│ └───Midterm1
│ │ Midterm1Review.pdf
│ │ Midterm1Review_myanswers.docx
| | ...
│
1.3 Header
At the beginning of any R script, you should have a standard header that you use across all scripts that clears the workspace, loads/installs packages as necessary, sets the working directory, etc. Here is an example that you can copy paste to the header of any script that you use:
###############################################################################
# list the packages we need and loads them, installs them automatically if we don't have them
# add any package that you need to the list
<- c('glue', 'dplyr','readxl', 'ggplot2','tidyr','AER','scales','mvtnorm',
need 'stargazer','httr')
<- need %in% rownames(installed.packages())
have if(any(!have)) install.packages(need[!have])
invisible(lapply(need, library, character.only=T))
# To set up the working directory
getwd()
setwd(getwd()) #change getwd() here is you need to set a different working directory
#this clears the workspace
rm(list = ls())
###############################################################################
When coding, use relative references to files. Typically, any script will begin looking for files in the working directory. At any time you can type getwd()
on your Rstudio console to see the current working directory. The header above automatically sets the working directory to the folder that the R script is included. For example, if you are working on Lastname_Firstname_Assignment1_EC282.R
script and need to load file dataset1name.Rda
into an object, then you would simply run:
load(dataset1name.Rda)
However, if you were working in the same .R file, and needed to access dataset2name.Rda
, you would need to point the program to a directory outside the current working directory – so, you go up one level, over one folder, and look there:
load(../Assignment2/dataset2name.Rda)
When learning R, the most important skill that you need to acquire is to be able to google your problem. There is probably not a single R question that you have yet has not been answered on Stack Overflow.