Imagine you have a vector of numbers and you want to create a new vector where each number is replaced by 'high' if it's greater than 10, and 'low' otherwise. How would you do this in R?

  • ifelse(numbers > 10, 'high', 'low')
  • if (numbers > 10) { 'high' } else { 'low' }
  • if (numbers > 10) 'high' else 'low'
  • ifelse(numbers > 10, 'low', 'high')
To create a new vector where each number is replaced by 'high' if it's greater than 10, and 'low' otherwise, you can use the ifelse() function. The syntax would be ifelse(numbers > 10, 'high', 'low'). This function performs a vectorized conditional operation and returns 'high' for numbers greater than 10, and 'low' for numbers less than or equal to 10.
Add your answer
Loading...

Leave a comment

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