0
0
R Programmingprogramming~30 mins

Vector arithmetic (element-wise) in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Vector arithmetic (element-wise)
📖 Scenario: You are working with two sets of numbers representing daily temperatures in two different cities. You want to compare them by adding, subtracting, multiplying, and dividing the temperatures element by element.
🎯 Goal: Build a simple R program that creates two numeric vectors, sets a length variable, performs element-wise arithmetic operations on the vectors, and prints the results.
📋 What You'll Learn
Create two numeric vectors named city1_temps and city2_temps with exact values
Create a variable n that stores the length of the vectors
Use element-wise arithmetic operations: addition, subtraction, multiplication, and division
Print the results of each arithmetic operation
💡 Why This Matters
🌍 Real World
Element-wise vector arithmetic is useful when comparing or combining data points from two related datasets, like temperatures from two cities over several days.
💼 Career
Data analysts and scientists often use vector operations to quickly process and analyze numerical data without writing loops.
Progress0 / 4 steps
1
Create two numeric vectors
Create a numeric vector called city1_temps with values c(20, 22, 19, 24, 21) and another numeric vector called city2_temps with values c(18, 21, 20, 23, 22).
R Programming
Need a hint?

Use the c() function to create vectors with the exact numbers given.

2
Create a length variable
Create a variable called n that stores the length of the vector city1_temps using the length() function.
R Programming
Need a hint?

Use length(city1_temps) to find how many elements are in the vector.

3
Perform element-wise arithmetic operations
Create four new vectors: sum_temps as the element-wise sum of city1_temps and city2_temps, diff_temps as the element-wise difference, prod_temps as the element-wise product, and quot_temps as the element-wise quotient.
R Programming
Need a hint?

Use +, -, *, and / operators directly on the vectors for element-wise operations.

4
Print the results
Print the vectors sum_temps, diff_temps, prod_temps, and quot_temps each on a separate line using the print() function.
R Programming
Need a hint?

Use print() for each vector to see the results on separate lines.