Suppose you're asked to write a function in R that takes a vector of numbers and returns a new vector containing only the even numbers. How would you do it?

  • Use the modulo operator (%%) to check if each element is divisible by 2
  • Use a for loop to iterate over each element and filter out the even numbers
  • Use the filter() function to extract the even numbers
  • Use the subset() function with a logical condition to filter the even numbers
To write a function in R that takes a vector of numbers and returns a new vector containing only the even numbers, you can use the modulo operator (%%) to check if each element is divisible by 2. By applying the modulo operator to the vector and comparing the result to 0, you can identify the even numbers and create a new vector with them.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *