Section 0: Setting up R
Before We Begin
Before our first section, please make sure you have working and up-to-date installations of R and RStudio. We will use both extensively in this course, so it is definitely worth doing right.
Installation
If you do not already have R and RStudio running on your computer, then you should install them both:
Step 1: Install R
Download and install R from the official CRAN repository:
- Windows: Download R for Windows
- macOS: Download R for macOS
- Linux: Download R for Linux
Step 2: Install RStudio
Download and install RStudio Desktop (free version):
- All platforms: Download RStudio Desktop
RStudio is a super helpful IDE (Integrated Development Environment) for R—it’s very helpful for both learning and using R.
Updating Existing Installations
If you already have R and RStudio on your computer, then you should make sure they are up to date:
- Current R version: 4.4.2 (2024-10-31) — “Pile of Leaves”
- Check your version by typing
versioninto the R console
- Check your version by typing
- Current RStudio version: 2024.12.0 or newer
- Check via: Help → About RStudio
Newer versions include bug fixes, performance improvements, and new features that make your coding experience smoother.
Additional Tools (Recommended)
LaTeX
I would recommend making sure you have a working installation of LaTeX for creating PDFs and mathematical typesetting:
Easy option: Install TinyTeX via R:
install.packages("tinytex") tinytex::install_tinytex()Full installation: Download MiKTeX (Windows) or MacTeX (macOS)
Online alternative: Overleaf (no installation needed, includes LaTeX tutorials)
Pandoc
Pandoc is usually included with RStudio, but you can verify by running in R:
rmarkdown::pandoc_version()If needed, download from: pandoc.org
Does It Work?
Let’s test your installation:
Open RStudio
In the console (bottom-left panel), type:
1 + 1Press Enter
Expected output:
[1] 2If you get [1] 2, congratulations! Everything is working correctly. ✅
If not, try reinstalling R and RStudio, or ask for help.
Resources for Learning R
I’ve compiled a large set of resources for R. You probably don’t need them all right now, but know they are there. However, the best resources for learning R are:
- Google (which will probably lead you to StackOverflow)
- Time (practice, practice, practice!)
- Your classmates (collaborative learning works wonders)
- Me (office hours, email, Slack/Teams)
Additional Resources
- R for Data Science (2e) by Hadley Wickham & Garrett Grolemund
- RStudio Cheatsheets
- Swirl - Interactive R tutorials
- DataCamp - Free intro course
- R-bloggers - Community blog posts
Basic Suggestions for Success
1. Use R Scripts, Not Just the Console
Nearly everything you do should be in an R script, as opposed to typing directly in the command line.
Why?
- You can return to your code later
- You can edit and improve your work
- You need to turn in R scripts for assignments
- It’s reproducible research!
How to create a script:
- File → New File → R Script (or
Cmd/Ctrl + Shift + N)
3. Be Consistent with Naming Conventions
Pick a style and stick to it:
# Camel Case (recommended for variables)
myVariableName <- 10
calculateTotalCost <- function(x) { x * 2 }
# Snake Case (also popular)
my_variable_name <- 10
calculate_total_cost <- function(x) { x * 2 }
# Choose ONE and be consistent!Avoid: - Starting variable names with numbers: 2021data ❌ - Using reserved words: if, for, function ❌ - Spaces in names: my variable ❌
4. Use Descriptive File Names
Be smart when naming files and folders:
Bad names:
cbn18319ddg890-7a.csv
untitled1.R
data.xlsx
final_FINAL_v2_REALFINAL.R
Good names:
CensusCountyDemographics.csv
01_DataCleaning.R
HouseholdIncomeSurvey_2024.xlsx
AnalyzeTaxReliefProgram.R
Two good naming conventions:
- Camel Case:
WhatAGreatFileName.csv - Snake Case:
what_a_great_file_name.csv
- Add dates in YYYY-MM-DD format:
2024-01-15_Analysis.R - Number files to show order:
01_Import.R,02_Clean.R,03_Analyze.R - Use descriptive prefixes:
fig_,tab_,data_
5. Organize Your Files and Use Smart Paths
Make your life easier by:
Organizing into folders:
MyProject/
├── Data/
│ ├── Raw/
│ └── Processed/
├── Scripts/
├── Output/
│ ├── Figures/
│ └── Tables/
└── README.md
Using paste0() or paste() for file paths:
# Instead of typing full paths every time:
data <- read.csv("/Users/david/Documents/Project/Data/census.csv")
# Use paste0() for flexibility:
project_path <- "/Users/david/Documents/Project"
data_path <- paste0(project_path, "/Data/census.csv")
data <- read.csv(data_path)
# Even better: use relative paths with here package
library(here)
data <- read.csv(here("Data", "census.csv"))We’ll see more examples of smart file organization and path management in Section 1.
Getting Started Checklist
Before the first class, make sure you can check all these boxes:
Need Help?
If you encounter any issues during installation or setup:
- Search Google with your error message
- Check StackOverflow for similar problems
- Ask classmates on our course forum/Slack
- Contact me during office hours or via email
Set up your R environment before the first assignment is due. Technical difficulties are easier to resolve when you’re not under time pressure.
Ready to Code!
Once everything is set up, you’re ready to start learning R! See you in Section 1.
Happy coding! 🚀
2. Comment Your Code
Good comments make your code understandable to others (and your future self):