In this tutorial, we will look at how to multiply two vectors (element-wise) in the R programming language with the help of some examples.
Elementwise Vector Multiplication in R
You can use the *
operator to multiply two vectors in R. Arithmetic operations on vectors are computed element-wise. That is when you multiply two vectors, the corresponding elements are multiplied together.
If the vectors are of the same length, corresponding elements (elements with the same index) are multiplied together. The following image illustrates this.
If the vectors are of different lengths, the shorter vector will be recycled to match the length of the longer vector after which the element-wise multiplication is computed. The following image shows how this is done.
Examples
Let’s look at some examples of multiplying two vectors (element-wise) in R.
Multiply two numeric vectors of the same length in R
First, let’s multiply two numeric vectors having the same length using the *
operator.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# create two vectors v1 <- c(1, 2, 3) v2 <- c(0, 1, 2) # multiply vectors v1 and v1 v <- v1 * v2 # display the resulting vector print(v)
Output:
[1] 0 2 6
Here, we multiply two vectors of length three. The resulting vector is also of length three with each element resulting from the corresponding elementwise multiplication of vectors v1 and v2.
Multiply two numeric vectors with different lengths in R
Let’s now multiply two numeric vectors having different lengths.
# create two vectors v1 <- c(1, 2, 3) v2 <- c(1, 1, 1, 1, 1) # multiply vectors v1 and v1 v <- v1 * v2 # display the resulting vector print(v)
Output:
Warning message in v1 * v2: “longer object length is not a multiple of shorter object length” [1] 1 2 3 1 2
Here, we multiply vector v1 (of length 3) with vector v2 (of length 5). You can see that the resulting vector is of length 5.
The shorter vector is recycled in order to compute the element-wise product with the longer vector. The values 1 and 2 are recycled to the index four and five respectively. Note that we get a warning that the longer vector’s length is not a multiple of the shorter vector’s length. This is why the shorter vector was not completely recycled, only the first two values were recycled to match the length of the longer vector.
Let’s look at another example. Here, the longer vector’s length is a multiple of the shorter vector’s length.
# create two vectors v1 <- c(1, 2, 3) v2 <- c(1, 1, 1, 1, 1, 1) # multiply vectors v1 and v1 v <- v1 * v2 # display the resulting vector print(v)
Output:
[1] 1 2 3 1 2 3
We get the resulting vector of length six (same as the length of the longer vector). Also, note that we don’t get any warnings here because the shorter vector is recycled once completely to compute the multiplication with the longer vector.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.