R — Get Started
Install R
First, install the R language itself:
- Go to cran.r-project.org
- Download the installer for your operating system
- Run it and accept the defaults
On Mac, you may also need to install XQuartz for some packages. On Linux, use your package manager.
Verify the installation
Open a terminal and type:
R --version
You should see the version number. The latest stable version is recommended.
Install RStudio
RStudio is the standard IDE for R development:
- Go to posit.co/download/rstudio-desktop
- Download the free version
- Install and launch
RStudio gives you a console, editor, file browser, plotting pane, variable viewer, and package manager — everything in one window.
The R console
When you open RStudio, you see the console. Type commands directly:
> 2 + 2
[1] 4
> "Hello, world!"
[1] "Hello, world!"
> seq(1, 5)
[1] 1 2 3 4 5
The > is the prompt. [1] indicates the first element of the output. R always shows element indices when printing vectors.
R scripts
Real work happens in scripts — saveable files with .R extension:
- In RStudio, click File → New File → R Script
- Type your code
- Press Ctrl+Enter (or Cmd+Enter on Mac) to run the current line
- Save with Ctrl+S
# my_first_script.R
# Assign values
name <- "Ada"
age <- 36
# Print
cat("Hello,", name, "! You are", age, "years old.\n")
Assignment
R uses <- for assignment (not =, though = also works):
x <- 5 # preferred style
y = 10 # also works, but not R convention
x <- 10 # reassign — x is now 10
The <- operator reads as "gets" — x gets the value 5. This is R's community convention. Some style guides allow = but <- is the standard.
Installing packages
R's power comes from packages. Install them from CRAN:
# Install a package (one-time)
install.packages("ggplot2")
# Load it for use
library(ggplot2)
install.packages()downloads and installs from CRANlibrary()loads the package into your current session- You only install once; you load every session
Essential packages
# Data manipulation
install.packages(c("dplyr", "tidyr", "readr"))
# Visualization
install.packages(c("ggplot2", "scales"))
# Data import
install.packages(c("readxl", "jsonlite"))
# Development
install.packages(c("devtools", "roxygen2"))
Getting help
# Search help
?mean # opens help page for mean
??linear regression # searches for "linear regression"
# Examples
example(plot) # runs the examples from the plot help page
# Arguments of a function
args(mean)
R's help system is comprehensive. Every function has a help page with description, usage, arguments, and examples.
Working directory
R runs commands relative to a working directory:
# Check current directory
getwd()
# Set a new directory
setwd("/path/to/project")
# List files
list.files()
In RStudio, set the working directory via Session → Set Working Directory.
Reading data
# CSV file
data <- read.csv("data.csv")
# Excel file (requires readxl)
library(readxl)
data <- read_excel("data.xlsx")
# Built-in datasets
data(mtcars)
head(mtcars)
R has built-in datasets for learning: mtcars, iris, airquality, faithful, and more.
Basic exploration
# Load a built-in dataset
data(iris)
# Structure
str(iris)
# Summary statistics
summary(iris)
# Dimensions
dim(iris) # 150 rows, 5 columns
# First few rows
head(iris)
# Column names
names(iris)
# Class
class(iris) # "data.frame"
RStudio shortcuts
| Shortcut | Action |
|---|---|
| Ctrl+Enter | Run current line |
| Ctrl+Shift+Enter | Run entire script |
| Ctrl+Enter | In console: run and advance |
| Ctrl+L | Clear console |
| Ctrl+Shift+C | Toggle comment |
| Tab | Autocomplete |
| F1 | Help for selected function |
Mini Practice
- Install R and RStudio, verify both are working
- In the console, calculate
100 * 365andsqrt(144) - Create a new R script and assign your name and age to variables
- Install the
ggplot2package and load it - Load the built-in
mtcarsdataset and runsummary(mtcars)
Next: R syntax rules →
Related Topics
Frequently Asked Questions about Get Started
What is Get Started in R?
Get Started is a fundamental concept in R. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Get Started?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Get Started.
Why is Get Started important in R?
Get Started is essential for R development. Understanding this concept will help you write better code and solve real-world problems more effectively.