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.
Loading...
Related Quiz
- What function is commonly used to find the maximum value in a vector in R?
- In R, to access the first element of an array named myarray, you would use ______.
- Can you calculate the standard deviation of a numeric vector in R?
- What is recursion in the context of R functions?
- Imagine you want to print a user-friendly message based on the value of a variable in R. How would you approach it?