Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Arithmetic Operations with Vectors | Basic Data Types and Vectors
R Introduction: Part I
course content

Conteúdo do Curso

R Introduction: Part I

R Introduction: Part I

1. Basic Syntax and Operations
2. Basic Data Types and Vectors
3. Factors

Arithmetic Operations with Vectors

Vectors in R offer a significant advantage due to their flexibility with various operations. For instance, if you have two vectors of the same length, you can easily perform addition or subtraction on an element-by-element basis.

Additionally, vectors can undergo arithmetic operations with single numbers, which apply the operation to each element of the vector. For example, let's create a vector with the numbers 10, 20, 30 and add 40, 25, 5 to each corresponding element:

123456
# Vectors a <- c(10, 20, 30) b <- c(40, 25, 5) # Addition c <- a + b c
copy

Now, let's go ahead and multiply each element by 2:

123456
a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b # Multiplication d <- c * 2 d
copy

R also provides a variety of aggregate and statistical functions. Let's explore two of the most common ones:

  • sum() - calculates and returns the sum of all vector elements;
  • mean() - computes and returns the average value of the vector elements.

We will proceed with our previous example and calculate the sum of all vector elements:

123456
a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b d <- c * 2 # Calculating the sum sum(d)
copy

Tarefa

Let's revisit our example with a small local store. This time we have data on the number of sales.

ItemPriceItems sold
Sofa$3405
Armchair$1507
Dining table$1153
Dining chair$4515
Bookshelf$1608
  1. Construct a vector called sold with the respective values from the Items sold column.
  2. Calculate the revenue by multiplying the prices and sold vectors and then output the result.
  3. Display the total sum of the revenue vector.

Tarefa

Let's revisit our example with a small local store. This time we have data on the number of sales.

ItemPriceItems sold
Sofa$3405
Armchair$1507
Dining table$1153
Dining chair$4515
Bookshelf$1608
  1. Construct a vector called sold with the respective values from the Items sold column.
  2. Calculate the revenue by multiplying the prices and sold vectors and then output the result.
  3. Display the total sum of the revenue vector.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 10
toggle bottom row

Arithmetic Operations with Vectors

Vectors in R offer a significant advantage due to their flexibility with various operations. For instance, if you have two vectors of the same length, you can easily perform addition or subtraction on an element-by-element basis.

Additionally, vectors can undergo arithmetic operations with single numbers, which apply the operation to each element of the vector. For example, let's create a vector with the numbers 10, 20, 30 and add 40, 25, 5 to each corresponding element:

123456
# Vectors a <- c(10, 20, 30) b <- c(40, 25, 5) # Addition c <- a + b c
copy

Now, let's go ahead and multiply each element by 2:

123456
a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b # Multiplication d <- c * 2 d
copy

R also provides a variety of aggregate and statistical functions. Let's explore two of the most common ones:

  • sum() - calculates and returns the sum of all vector elements;
  • mean() - computes and returns the average value of the vector elements.

We will proceed with our previous example and calculate the sum of all vector elements:

123456
a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b d <- c * 2 # Calculating the sum sum(d)
copy

Tarefa

Let's revisit our example with a small local store. This time we have data on the number of sales.

ItemPriceItems sold
Sofa$3405
Armchair$1507
Dining table$1153
Dining chair$4515
Bookshelf$1608
  1. Construct a vector called sold with the respective values from the Items sold column.
  2. Calculate the revenue by multiplying the prices and sold vectors and then output the result.
  3. Display the total sum of the revenue vector.

Tarefa

Let's revisit our example with a small local store. This time we have data on the number of sales.

ItemPriceItems sold
Sofa$3405
Armchair$1507
Dining table$1153
Dining chair$4515
Bookshelf$1608
  1. Construct a vector called sold with the respective values from the Items sold column.
  2. Calculate the revenue by multiplying the prices and sold vectors and then output the result.
  3. Display the total sum of the revenue vector.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 2. Capítulo 10
toggle bottom row

Arithmetic Operations with Vectors

Vectors in R offer a significant advantage due to their flexibility with various operations. For instance, if you have two vectors of the same length, you can easily perform addition or subtraction on an element-by-element basis.

Additionally, vectors can undergo arithmetic operations with single numbers, which apply the operation to each element of the vector. For example, let's create a vector with the numbers 10, 20, 30 and add 40, 25, 5 to each corresponding element:

123456
# Vectors a <- c(10, 20, 30) b <- c(40, 25, 5) # Addition c <- a + b c
copy

Now, let's go ahead and multiply each element by 2:

123456
a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b # Multiplication d <- c * 2 d
copy

R also provides a variety of aggregate and statistical functions. Let's explore two of the most common ones:

  • sum() - calculates and returns the sum of all vector elements;
  • mean() - computes and returns the average value of the vector elements.

We will proceed with our previous example and calculate the sum of all vector elements:

123456
a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b d <- c * 2 # Calculating the sum sum(d)
copy

Tarefa

Let's revisit our example with a small local store. This time we have data on the number of sales.

ItemPriceItems sold
Sofa$3405
Armchair$1507
Dining table$1153
Dining chair$4515
Bookshelf$1608
  1. Construct a vector called sold with the respective values from the Items sold column.
  2. Calculate the revenue by multiplying the prices and sold vectors and then output the result.
  3. Display the total sum of the revenue vector.

Tarefa

Let's revisit our example with a small local store. This time we have data on the number of sales.

ItemPriceItems sold
Sofa$3405
Armchair$1507
Dining table$1153
Dining chair$4515
Bookshelf$1608
  1. Construct a vector called sold with the respective values from the Items sold column.
  2. Calculate the revenue by multiplying the prices and sold vectors and then output the result.
  3. Display the total sum of the revenue vector.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Vectors in R offer a significant advantage due to their flexibility with various operations. For instance, if you have two vectors of the same length, you can easily perform addition or subtraction on an element-by-element basis.

Additionally, vectors can undergo arithmetic operations with single numbers, which apply the operation to each element of the vector. For example, let's create a vector with the numbers 10, 20, 30 and add 40, 25, 5 to each corresponding element:

123456
# Vectors a <- c(10, 20, 30) b <- c(40, 25, 5) # Addition c <- a + b c
copy

Now, let's go ahead and multiply each element by 2:

123456
a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b # Multiplication d <- c * 2 d
copy

R also provides a variety of aggregate and statistical functions. Let's explore two of the most common ones:

  • sum() - calculates and returns the sum of all vector elements;
  • mean() - computes and returns the average value of the vector elements.

We will proceed with our previous example and calculate the sum of all vector elements:

123456
a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b d <- c * 2 # Calculating the sum sum(d)
copy

Tarefa

Let's revisit our example with a small local store. This time we have data on the number of sales.

ItemPriceItems sold
Sofa$3405
Armchair$1507
Dining table$1153
Dining chair$4515
Bookshelf$1608
  1. Construct a vector called sold with the respective values from the Items sold column.
  2. Calculate the revenue by multiplying the prices and sold vectors and then output the result.
  3. Display the total sum of the revenue vector.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 10
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt