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
Now, let's go ahead and multiply each element by 2
:
123456a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b # Multiplication d <- c * 2 d
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:
123456a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b d <- c * 2 # Calculating the sum sum(d)
Swipe to start coding
Let's revisit our example with a small local store. This time we have data on the number of sales.
Item | Price | Items sold |
---|---|---|
Sofa | $340 | 5 |
Armchair | $150 | 7 |
Dining table | $115 | 3 |
Dining chair | $45 | 15 |
Bookshelf | $160 | 8 |
- Construct a vector called
sold
with the respective values from the Items sold column. - Calculate the
revenue
by multiplying theprices
andsold
vectors and then output the result. - Display the total sum of the
revenue
vector.
Solución
¡Gracias por tus comentarios!
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Can you explain how the mean() function works with vectors in R?
What happens if the vectors are of different lengths?
Can you show more examples of aggregate functions in R?
Awesome!
Completion rate improved to 2.27
Arithmetic Operations with Vectors
Desliza para mostrar el menú
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
Now, let's go ahead and multiply each element by 2
:
123456a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b # Multiplication d <- c * 2 d
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:
123456a <- c(10, 20, 30) b <- c(40, 25, 5) c <- a + b d <- c * 2 # Calculating the sum sum(d)
Swipe to start coding
Let's revisit our example with a small local store. This time we have data on the number of sales.
Item | Price | Items sold |
---|---|---|
Sofa | $340 | 5 |
Armchair | $150 | 7 |
Dining table | $115 | 3 |
Dining chair | $45 | 15 |
Bookshelf | $160 | 8 |
- Construct a vector called
sold
with the respective values from the Items sold column. - Calculate the
revenue
by multiplying theprices
andsold
vectors and then output the result. - Display the total sum of the
revenue
vector.
Solución
¡Gracias por tus comentarios!
Awesome!
Completion rate improved to 2.27single