R programming basics

Author

Abraham Olvera Barrios & Alasdair Warwick

Published

March 1, 2025

TLDR
  • R can be used as a calculator for basic arithmetic operations.
  • Variables are assigned using the <- operator.
  • Data frames are used to store tabular data.
  • Understanding different object types in R is crucial for data manipulation.
  • Logical and comparison operators help in making decisions.
  • Loops and functions are essential for repetitive tasks and code organization.

Welcome to R programming! This guide will introduce you to the basics of R, starting with simple calculations, then moving on to assigning variables, working with data frames, and exploring different object types.

Introduction

Using R as a Calculator

R can be used to perform basic arithmetic operations just like a calculator.

Assigning Variables

In R, you can assign values to variables using the <- operator.

Data Frames

Data frames are a fundamental data structure in R, used to store tabular data. Each column in a data frame is a vector of values of the same type. This structure allows for efficient data manipulation and analysis. Data frames are actually a special type of list, where each item is a vector of the same length. Understanding the different types of variables in R is crucial for working with data frames. So far, we have only mentioned numeric types, but we will cover other types now!

Objects

Basic Types

Scalars

Vectors

Combine objects of the same type in a vector

Access items using square brackets

Lists

Combine objects of different types in a list

List elements can be named

Lists can contain lists (nested lists)

Subset lists using square brackets

Access list items with double square brackets or $

Data Frames

A special type of list, where each item is a vector of the same length

Subset with square brackets

Access columns with double square brackets, or $. Both methods return a vector

Functions

Functions are blocks of code

Functions may be called to execute the code they contain

Arguments extend function utility

Conditions

Comparison operators

Operator Description Example Result
== Equal to 5 == 5 TRUE
!= Not equal to 5 != 4 TRUE
< Less than 3 < 5 TRUE
> Greater than 3 > 5 FALSE
<= Less than or equal to 3 <= 3 TRUE
>= Greater than or equal to 3 >= 5 FALSE

Logical operators

Operator Description Example Result
& Element-wise AND c(TRUE, FALSE) & c(TRUE, TRUE) TRUE FALSE
| Element-wise OR c(TRUE, FALSE) | c(FALSE, TRUE) TRUE TRUE
! NOT !TRUE FALSE
%in% Checks if elements in one vector are present in another vector 2 %in% c(1, 2, 3) TRUE

Examples

if, else if, else

Data wrangling

With the foundational knowledge we’ve covered, we can now begin to manipulate our data frames effectively.

Filtering rows

Use a data mask

Creating columns

Loops

Loops are an essential concept in programming, allowing you to automate repetitive tasks efficiently. They enable you to execute a block of code multiple times, which can save time and reduce errors in your code.

For loops

Do something for each item in an interable (vector/list)

Can also loop through by index using seq_along()

A common pattern is to create an empty list to hold the results from your loop

While loops

Continue looping while a certain condition is met (beware infinite loops!)

Exercises

Now it’s time to practice what you’ve learned. Try the following exercises. Hints are available if you get stuck, and your answers will be graded.

Exercise 1: Basic Arithmetic

Calculate the sum of 15 and 25.

Hint 1

Remember to use the + operator for addition.

15 + 25
Fully worked solution:

Simply add the two numbers:

15 + 25

Exercise 2: Assigning Variables

Assign the value 100 to a variable named a and the value 200 to a variable named b. Then calculate their sum.

Hint 1

Use the <- operator to assign values to variables.

a <- 100
b <- 200
Fully worked solution:

Assign the values and calculate the sum:

a <- 100
b <- 200
a + b

Exercise 3: Logical Operators

Check if the number 5 is greater than 3 and less than 10.

Hint 1

Use the & operator to combine two logical conditions.

5 > 3 & 5 < 10
Fully worked solution:

Combine the two conditions using the & operator:

5 > 3 & 5 < 10

Exercise 4: For Loop

Write a for loop to create a list containing the numbers 1 to 5.

Hint 1

Use the c() function to append to a list.

# empty list
result <- list()

# append `1` to the empty list using `c()`
result <- c(result, 1)
Fully worked solution:

Use c() to grow the list:

result <- list()
for (i in 1:5) {
  result <- c(result, i)
}
result

Exercise 5: While Loop

Write a while loop to create a list containing the numbers 1 to 5.

Hint 1

Increment the value of x by 1 in each iteration.

x <- 1

# the loop will complete when the value of `x` reaches 6
while (x <= 5) {
  x <- x + 1
}
Fully worked solution:

Write the while loop to increment x and grow the list:

result <- list()
x <- 1
while (x <= 5) {
  result <- c(result, x)
  x <- x + 1
}
result

Exercise 6: Data Frames - filter rows

Filter this data frame for people aged 30 or older.

Hint 1

Use square brackets to filter rows.

# Filter for rows where name is "Alice"
df[df$Name == "Alice", ]
Fully worked solution:

Filter the data frame using square brackets:

df[df$Age >= 30, ]