Introduction to R

Author

Dr. Anisha Singh

What is R?

R is a free, open-source programming language and computing environment built for statistics and data analysis. It is widely used across psychology, education, public health, and the social sciences — and it is what we will use throughout this course.

Unlike point-and-click software such as SPSS, R is a scripting language. You write a sequence of instructions, and R executes them in order. This takes practice at first, but the payoff is significant: your analyses are reproducible, shareable, and easy to modify.


Why Use R?

  • Free. R costs nothing and runs on Mac, Windows, and Linux.
  • Reproducible. Your entire analysis lives in a script you can re-run, share, or hand to a reviewer.
  • Powerful and flexible. From t-tests to multilevel models to machine learning — R handles it all.
  • Standard in research. R is the dominant tool in academic psychology and education research. Learning it now is an investment you will use throughout your career.

Installing R and RStudio

You need two things. Install them in order.

1. R — the language itself
Download from cran.r-project.org. Choose your operating system and install the current release.
2. RStudio — the editor that makes R easier to use
Download the free RStudio Desktop from posit.co/download/rstudio-desktop. Always open RStudio, not R directly.

Think of R as the engine and RStudio as the dashboard.

The RStudio Interface

When RStudio opens you will see four panes:

Pane Purpose
Source (top-left) Write and save your scripts here
Console (bottom-left) Where R runs code and shows output
Environment (top-right) Objects currently stored in memory
Files / Plots / Help (bottom-right) Browse files, view plots, read documentation

Write your code in the Source pane and run it with Ctrl+Enter (Windows) or Cmd+Return (Mac). Results appear in the Console.


R as a Calculator

The simplest thing R can do is arithmetic. Type any expression and press Enter:

1 + 1
[1] 2

The [1] at the start of every output line just means “first element of the result.” You will see it everywhere.

10 - 3        # subtraction
4 * 5         # multiplication
20 / 4        # division
2 ^ 8         # exponentiation  →  256
sqrt(144)     # square root     →  12
abs(-7)       # absolute value  →  7
TipThe # symbol marks a comment

Everything after # on a line is a comment — R ignores it when running the code. Use comments freely to explain what each line does. Your future self will thank you.


Objects

In R, you can store any value in a named object using the assignment arrow <-:

X <- 8

Now X holds the value 8. Call it by name to see it:

X
[1] 8

You can use objects in calculations just like raw numbers:

X + 2       # 10
X * X       # 64
sqrt(X)     # 2.828427

Object names are case-sensitive: X and x are two different objects.

Vectors

A vector is a sequence of values stored together. Create one with c() — short for “combine”:

Y <- c(1, 2, 3, 4, 5)
Y
[1] 1 2 3 4 5

Operations on a vector apply to every element at once:

Y + 10       # [1] 11 12 13 14 15
Y * 2        # [1]  2  4  6  8 10

Vectors can also hold text (called character strings):

names <- c("Alice", "Bob", "Carol")
names
[1] "Alice" "Bob"   "Carol"

Matrices

A matrix is a two-dimensional table of values — rows and columns. Create one with matrix():

Z <- matrix(1:9, nrow = 3, ncol = 3)
Z
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

Values fill in column by column by default. You can transpose with t(Z).


Classes

Every object in R has a class — a label that tells R what kind of thing it is and which operations are valid. You can check it with class():

class(8)            # "numeric"
class("hello")      # "character"
class(TRUE)         # "logical"
class(c(1,2,3))     # "numeric"
NoteWhy classes matter

Almost every error in R comes down to a class mismatch — trying to do math on a character vector, or passing a data frame to a function that expects a single number. Checking the class of your objects is the first debugging step.


A First Look at Data

Real datasets in R are stored as data frames — tables with rows (observations) and columns (variables). R ships with several built-in datasets for practice.

mtcars has data on 32 cars:

head(mtcars)       # first 6 rows

Use $ to pull out a single column by name:

mtcars$cyl         # cylinder counts for all 32 cars
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

A few useful inspection functions:

dim(mtcars)        # rows, columns:  32  11
names(mtcars)      # variable names
class(mtcars)      # "data.frame"
class(mtcars$mpg)  # "numeric"

Getting Help

When you are unsure how a function works, prefix it with ?:

?mean
?sqrt
?matrix

The Help pane opens with the function’s documentation: description, arguments, and worked examples. ??keyword searches all installed documentation.


What’s Next

This page covers R’s core building blocks. When you are comfortable with objects, vectors, and data frames, continue to the applied script:

Descriptives, Correlation & Regression — loading real data, computing descriptive statistics with psych::describe(), running cor.test(), and fitting a multiple regression with lm().

That script uses a real psychology dataset and walks through a complete analysis from import to interpretation.