</>
Skip to content
R lessons (3/41)

R — Get Started

Install R

First, install the R language itself:

  1. Go to cran.r-project.org
  2. Download the installer for your operating system
  3. 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:

  1. Go to posit.co/download/rstudio-desktop
  2. Download the free version
  3. 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:

  1. In RStudio, click File → New File → R Script
  2. Type your code
  3. Press Ctrl+Enter (or Cmd+Enter on Mac) to run the current line
  4. 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 CRAN
  • library() 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

ShortcutAction
Ctrl+EnterRun current line
Ctrl+Shift+EnterRun entire script
Ctrl+EnterIn console: run and advance
Ctrl+LClear console
Ctrl+Shift+CToggle comment
TabAutocomplete
F1Help for selected function

Mini Practice

  1. Install R and RStudio, verify both are working
  2. In the console, calculate 100 * 365 and sqrt(144)
  3. Create a new R script and assign your name and age to variables
  4. Install the ggplot2 package and load it
  5. Load the built-in mtcars dataset and run summary(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.