Instruction: Write a function in R that takes two vectors as input and returns a vector containing the products of corresponding elements. Demonstrate your function with an example using the vectors (1, 2, 3) and (4, 5, 6).
Context: This question assesses the candidate's understanding of basic vector operations in R, including element-wise operations. It tests the candidate’s ability to write functions for simple tasks, which is a fundamental skill in data manipulation in R.
Official answer available
Preview the opening of the answer, then unlock the full walkthrough.
The task is to write a function in R that takes two vectors as input and returns a single vector. This resultant vector should contain the products of corresponding elements from the input vectors. To put it simply, if we have two vectors, the first with elements (a, b, c) and the second with elements (x, y, z), our function should return a vector with elements (ax, by, c*z). This operation is known as element-wise multiplication.
Let's move on to drafting the function. I'll call this function elementwise_product. Its inputs will be two vectors, let's name them vector1 and...