Video Notes
New to R? The above video get you up and running with R and RStudio fast. Below is a summary of key points and code samples.
Installing
Visit https://posit.co/download/rstudio-desktop/ and follow the links to download installers for both R and RStudio.
The R installer includes a simple R console and editor you can use to write and run R code. However, many R developers opt to use RStudio, a robust code editor program designed to work with R.
RStudio depends on the R install, so that is why we’re installing both.
RStudio
RStudio is split up into 4 panes, and each pane has different tabs you can access.
- On the top left pane, I have the code editor active with a .R script open.
- On the top right pane, I can see any values currently set in my environment.
- On the bottom left is a R console where I can execute R code and see output.
- On the bottom right is a file browser. In the video, I also access the Help and Plots tab from this pane.
Running code
There are three primary ways you’ll execute code when working in RStudio:
- Via Run to execute specific lines/blocks of code in the currently open file
- Useful if you want to test incremental changes without having to re-run the whole file. Be aware of order in which you execute code ensuring any values you are trying to access are loaded in the environment.
- Via Source to execute all the code in the currently open file
- Directly in the console
- Useful for quick "disposable" commands that you want to test out but don’t need to save in your script.
To speed up your workflow, get familiar with the following keyboard shortcuts:
Mac
-
Run: command + return
-
Source: shift + command + S
Windows
-
Run: Ctrl + Enter
-
Source: Ctrl + Shift + S
Full RStudio keyboard shortcut reference here...
Basic Syntax
# Assigning values
x <- 10
# Printing output
print(x)
# Load the documentation for a function
?plot
# See all available built-in R functions
ls("package.base")
Simple Data Visualization Example via plot function
x_values <- 1:5
y_values <- x_values^2
plot(x_values, y_values,
xlab = "X Values",
ylab = "Y Values",
col = "blue",
main = "Example Plot")
Packages Example via ggplot2
The site r-packages-io is a useful resource for browsing/discovering packages. Below is an example installing and using the package ggplot2.
Install the ggplot2 package:
install.packages("ggplot2")
Load the library:
library(ggplot2)
Example usage:
# Define a data frame of example data
df <- data.frame(
x = c(1, 2, 3, 4, 5),
y = c(2, 4, 6, 8, 10)
)
# Generate visualization
ggplot(df, aes(x = x, y = y)) +
geom_point(color = "blue", size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(title = "Scatter Plot with Regression Line", x = "X-Axis", y = "Y-Axis") +
theme_minimal()
Road map of what to learn next...
Common Data Types
num_var <- 10.5 # Numeric (double)
char_var <- "Hello" # Character
bool_var <- TRUE # Logical (Boolean)
Data Structures
# Vectors (1D, Same-type Data)
vec <- c(1, 2, 3, 4, 5) # Numeric vector
char_vec <- c("A", "B", "C") # Character vector
log_vec <- c(TRUE, FALSE, TRUE) # Logical vector
# Vector operations
vec*2 # x2 is applied to each element in the vector, yielding 2 4 6 8 10
## Lists
my_list <- list(10, "R", TRUE, c(1, 2, 3)) # Mixed data types
my_list[[2]] # Access second element
## Matrices (2D, Same-type Data)
mat <- matrix(1:9, nrow=3, ncol=3)
mat[2,3] # Access row 2, column 3
## Data Frames (Tabular Data, Mixed Column Types)
df <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30), Score = c(90.5, 88.2))
head(df) # View first few rows
df$Name # Access column
## Factors (Categorical Data)
colors <- factor(c("red", "blue", "red", "green"))
levels(colors) # Check factor levels
Programming Structures
# Conditionals
if (x > 10) {
print("Greater than 10")
} else {
print("10 or less")
}
# for loops
for (i in 1:5) {
print(i)
}
# while loops
x <- 1
while (x <= 5) {
print(x)
x <- x + 1
}
# functions
my_function <- function(a, b) {
return(a + b)
}
my_function(5, 10) # Returns 15
Reading and Writing Files
df <- read.csv("data.csv") # Read CSV
write.csv(df, "output.csv") # Write CSV