Skip to main content
eLearner.app
Module 1 · Lesson 1 of 21/10 in the course~10 min
Module lessons (1/2)

Variables and Vectors

R is a programming language and software environment specialized in statistical data analysis, data visualization, and machine learning.

Unlike other general-purpose languages, R was designed by statisticians for statisticians, making vector and matrix manipulation and aggregation operations extremely intuitive.

Variables and Assignment

In R, the standard convention for assigning a value to a variable is to use the left arrow operator <-, composed of the less-than character < followed by a hyphen -.

Although the = operator is valid in most contexts, the R community prefers <- to clearly distinguish variable assignment from passing arguments inside functions.

Code
# Assign the value 42 to the variable age
age <- 42

# We can print the value using the print() function
print(age)

Vectors

The fundamental data structure in R is the vector. A vector is an ordered collection of elements of the same type. In R, even a single number (a scalar) is technically treated as a vector of length 1.

To create a vector with multiple elements, use the concatenation function c():

Code
# Create a vector with the first four even numbers
even_numbers <- c(2, 4, 6, 8)
print(even_numbers)

Vectorized Operations

R supports vectorized operations. When you apply an arithmetic operator to a vector, R applies the operation to each element of the vector individually (element-by-element):

Code
prices <- c(10, 20, 30)

# Multiply each price by 2
doubled_prices <- prices * 2
# The result will be c(20, 40, 60)

If we operate on two vectors of the same length, the operation is executed element-by-element:

Code
v1 <- c(1, 2, 3)
v2 <- c(10, 20, 30)
v3 <- v1 + v2
# The result will be c(11, 22, 33)

Sequences and Vector Names

R provides powerful tools for generating sequences of numbers. In addition to the : operator (e.g., 1:10), we can use the seq() function to define a custom increment:

Code
# Generates numbers from 1 to 5 with an increment of 0.5
custom_seq <- seq(from = 1, to = 5, by = 0.5)

Furthermore, elements of a vector can have associated names that make them easier to identify, which can be set using the names() function:

Code
temperatures <- c(22, 24)
names(temperatures) <- c("Mon", "Tue")

Try it yourself

Exercise#r.m1.l1.e1
Attempts: 0Loading…

Assign the value 10 to the variable x and the value 20 to the variable y using the <- operator.

Loading editor…
Show hint

Scrivi: x <- 10 e nella riga successiva: y <- 20

Solution available after 3 attempts

Exercise#r.m1.l1.e2
Attempts: 0Loading…

Create a vector containing the values 5, 10, 15, 20 and assign it to the variable named numbers.

Loading editor…
Show hint

Usa la funzione c() in questo modo: numbers <- c(5, 10, 15, 20)

Solution available after 3 attempts

Exercise#r.m1.l1.e3
Attempts: 0Loading…

Given the vectors v1 and v2, calculate their sum and assign the result to the variable total.

Loading editor…
Show hint

Somma direttamente i due vettori: total <- v1 + v2

Solution available after 3 attempts

Exercise#r.m1.l1.e4
Attempts: 0Loading…

Create a sequence of numbers from 1 to 10 with an increment of 2 using the seq() function and assign it to the variable my_seq.

Loading editor…
Show hint

Use the seq() function: my_seq <- seq(from = 1, to = 10, by = 2)

Solution available after 3 attempts

Exercise#r.m1.l1.e5
Attempts: 0Loading…

Given the vector sales <- c(100, 150), assign the names 'Mon' and 'Tue' to the vector using the names() function.

Loading editor…
Show hint

Set names(sales) <- c('Mon', 'Tue')

Solution available after 3 attempts