R Programming: Test Whether Vector elements are greater than 10
Write a R program to test whether the value of the element of a given vector greater than 10 or not. Return TRUE or FALSE.
Sample Solution :
R Programming Code :
# Create a vector 'v' with the elements 15, 26, 9, 7, 10, 0, 9, 15
v = c(15, 26, 9, 7, 10, 0, 9, 15)
# Print a message indicating the original vector
print("Original vector:")
# Print the content of the vector 'v'
print(v)
# Print a message indicating the result of the comparison
print("Test whether the value > 10 or not:")
# Check which elements of 'v' are greater than 10 and print the logical result (TRUE or FALSE)
print(v > 10)
Output:
[1] "Original vector:" [1] 15 26 9 7 10 0 9 15 [1] "Test whether the value > 10 or not:" [1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
Explanation:
- Create a vector:
- v = c(15, 26, 9, 7, 10, 0, 9, 15)
A vector v is created containing the numeric elements 15, 26, 9, 7, 10, 0, 9, and 15. - Print message (Original vector):
- print("Original vector:")
Prints a message indicating that the following output will display the original vector. - Display the vector:
- print(v)
Displays the content of the vector v. - Print message (Test value > 10):
- print("Test whether the value > 10 or not:")
Prints a message indicating that the following output will show whether each element in the vector is greater than 10. - Test and print the result:
- print(v > 10)
Checks each element in the vector v to see if it is greater than 10. The result is a logical vector of TRUE or FALSE values, indicating whether each element meets the condition
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to combines two given vectors by columns, rows.
Next: Wrtie a R program to add 3 to each element in a given vector. Print the original and new vector.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics