R Programming Tutorial
Economic Statistics
Minseog Oh
Learning Goals
After this tutorial, you should be able to:
• Open RStudio and run code in the Console.
• Use basic R objects and inspect data frames.
• Load CSV files for this course.
• Create simple summaries and plots in base R.
• Draw your first scatter plot with ggplot2.
Audience: complete beginners (no prior programming required).
2 / 14
Install Flow (Windows First)
1. Install R from CRAN ([Link]
2. Install RStudio Desktop from Posit
([Link]
3. Open RStudio and make a new R script file.
3 / 14
RStudio Interface
Source: script editor Console: run commands Environment: objects (variables) Files/Plots/Help: outputs
4 / 14
Working Directory, Path, & Relative Path
# Working directory = R’s base folder for file reading/writing
getwd()
# Why it matters: wrong folder -> "cannot open file"
setwd("C:/Users/User/Project") # Windows
# setwd("/home/user/Project") # macOS/Linux
# Relative path symbols (\textbf{Relative path}: location from the working directory.)
# (\textbf{Absolute path}: full location from root (e.g., C:/Users/User/Project).)
"." # current folder
".." # parent folder (one level up)
# Examples from the working directory
[Link]("./data/[Link]")
# or
# setwd("./data")
# [Link]("[Link]")
• Working directory: the default folder R looks at to read/write files.
• Why needed: if this base folder is wrong, file loading often fails.
•
5 / 14
First Commands and Objects
> 2 + 3
> x <- c(1, 3, 5, 7)
> length(x)
> class(x)
> z <- 1:10
> z
• <- assigns values to an object.
• c(...) creates a vector.
• a:b creates an integer sequence from a to b.
Mini task: create y <- c(2,4,6,8) and check length(y).
6 / 14
Data Frame Basics
> head(mtcars) # first 6 rows
> nrow(mtcars) # number of rows (observations)
> ncol(mtcars) # number of columns (variables)
> names(mtcars) # column/variable names
> summary(mtcars) # quick summary statistics
• mtcars is a built-in dataset in R.
• A data frame is a table (rows = observations, columns = variables).
Mini task: identify one numeric variable from names(mtcars).
7 / 14
Load Course CSV #1
> data_dir <- "./data"
> lightbeer <- [Link](paste0(data_dir, "/light_beer_preference_survey.csv"))
> head(lightbeer)
• paste0(a, b) joins text with no separator (used to build a file path).
• [Link](...) loads a CSV into a data frame.
• head(...) quickly checks if data loaded correctly.
8 / 14
Categorical Summary (Base R)
> brand_freq <- table(lightbeer$Brand) # $ = select a column
> brand_freq
> brand_rel <- brand_freq / sum(brand_freq)
> barplot(brand_freq, main="Brand Frequency")
> pie(brand_freq, main="Brand Share")
• data$variable means: use column variable from data frame data.
Mini task: explain the difference between brand freq and brand rel.
9 / 14
Load Course CSV #2
> longdist <- [Link](paste0(data_dir, "/long_distance_telephone_bills.csv"))
> head(longdist)
> bills <- longdist$Bills
• bills is now a numeric vector for analysis.
10 / 14
Numeric Summary and Plots (Base R)
> mean(bills); median(bills); sd(bills); quantile(bills)
> hist(bills, breaks=seq(0, 120, 15), col="lightblue", main="Histogram of Bills")
> boxplot(bills, horizontal=TRUE, col="lightblue", main="Boxplot of Bills")
Mini task: compare mean and median. Is the distribution symmetric?
11 / 14
Install and Load ggplot2
> [Link]("ggplot2")
> library(ggplot2)
12 / 14
First ggplot Example
> ggplot(mtcars, aes(x=wt, y=mpg)) +
+ geom_point(color="steelblue") +
+ geom_smooth(method="lm", se=FALSE, color="firebrick")
• wt: car weight, mpg: miles per gallon.
• This plot shows the relationship and a fitted linear trend.
13 / 14
Common Errors and Debugging Checklist
• object not found
• Run previous lines first. Check spelling.
• cannot open file
• Verify data dir and file names.
• there is no package called ...
• Install with [Link]("...") then load with library(...).
Run code top-to-bottom in one script. This prevents most beginner errors.
14 / 14