Bird
Raised Fist0
R Programmingprogramming~10 mins

Comparison operators in R Programming - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if x is equal to 10.

R Programming
x <- 10
result <- x [1] 10
print(result)
Drag options to blanks, or click blank then click option'
A=
B!=
C<-
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using = instead of == for comparison.
Using <- which is for assignment, not comparison.
2fill in blank
medium

Complete the code to check if y is greater than 5.

R Programming
y <- 7
if (y [1] 5) {
  print("y is greater than 5")
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of >.
Using == which checks equality, not greater than.
3fill in blank
hard

Fix the error in the code to check if z is not equal to 0.

R Programming
z <- 3
if (z [1] 0) {
  print("z is not zero")
}
Drag options to blanks, or click blank then click option'
A<
B==
C!=
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using = which is for assignment, not comparison.
Using == which checks equality, not inequality.
4fill in blank
hard

Complete the code to create a dictionary of words and their lengths, but only for words longer than 3 letters.

R Programming
words <- c("cat", "house", "dog", "elephant")
lengths <- list()
for (word in words) {
  if (nchar(word) [1] 3) {
    lengths[[word]] <- nchar(word)
  }
}
Drag options to blanks, or click blank then click option'
A>
B<
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the condition.
Adding an operator when assigning the length.
5fill in blank
hard

Fill both blanks to create a named list of uppercase words and their lengths, only if length is at least 4.

R Programming
words <- c("tree", "sun", "flower", "sky")
result <- list()
for (w in words) {
  if (nchar(w) [1] 3) {
    result[[[2]]] <- nchar(w)
  }
}
Drag options to blanks, or click blank then click option'
A>
Btoupper(w)
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the condition.
Not converting the word to uppercase for the name.
Adding an operator when assigning the length.

Practice

(1/5)
1. Which of the following comparison operators in R checks if two values are equal?
easy
A. !=
B. =
C. <>
D. ==

Solution

  1. Step 1: Understand equality operator in R

    In R, the operator to check if two values are equal is ==.
  2. Step 2: Differentiate from other operators

    = is for assignment, != means not equal, and <> is not valid in R.
  3. Final Answer:

    == -> Option D
  4. Quick Check:

    Equality operator = == [OK]
Hint: Use double equals (==) to test equality in R [OK]
Common Mistakes:
  • Using single = instead of ==
  • Confusing != with equality
  • Using <> which is invalid in R
2. Which of the following is the correct syntax to check if variable x is NOT equal to 10 in R?
easy
A. x != 10
B. x <> 10
C. x =! 10
D. x !== 10

Solution

  1. Step 1: Identify the not equal operator in R

    R uses != to check if two values are not equal.
  2. Step 2: Check syntax correctness

    <> and !== are not valid in R, and =! is incorrect syntax.
  3. Final Answer:

    x != 10 -> Option A
  4. Quick Check:

    Not equal operator = != [OK]
Hint: Use != to check inequality in R [OK]
Common Mistakes:
  • Using <> which is invalid in R
  • Writing =! instead of !=
  • Using !== which is from other languages
3. What is the output of the following R code?
x <- 5
y <- 7
x > y
medium
A. FALSE
B. TRUE
C. 5
D. Error

Solution

  1. Step 1: Compare values of x and y

    x is 5 and y is 7, so 5 > 7 is being checked.
  2. Step 2: Evaluate the comparison

    Since 5 is not greater than 7, the expression returns FALSE.
  3. Final Answer:

    FALSE -> Option A
  4. Quick Check:

    5 > 7 = FALSE [OK]
Hint: Compare numbers directly; 5 > 7 is false [OK]
Common Mistakes:
  • Confusing > with <
  • Expecting numeric output instead of logical
  • Thinking it causes an error
4. Identify the error in this R code snippet:
if (x = 10) { print("Yes") }
medium
A. No error, code is correct
B. Missing curly braces
C. Using = instead of ==
D. print() is incorrect function

Solution

  1. Step 1: Check the condition syntax in if statement

    In R, to compare values, use ==, not =.
  2. Step 2: Understand the difference between assignment and comparison

    = assigns a value, but == tests equality. Using = in if condition causes an error or unexpected behavior.
  3. Final Answer:

    Using = instead of == -> Option C
  4. Quick Check:

    Comparison needs ==, not = [OK]
Hint: Use == for comparison inside if() [OK]
Common Mistakes:
  • Using = instead of ==
  • Ignoring syntax errors from wrong operator
  • Thinking = works for comparison
5. You want to filter a vector v <- c(3, 5, 7, 9) to keep only values greater than or equal to 5. Which R expression correctly uses comparison operators to do this?
hard
A. v[v => 5]
B. v[v >= 5]
C. v[v = 5]
D. v[v >== 5]

Solution

  1. Step 1: Understand filtering with comparison

    To keep values >= 5, use the operator >= inside the indexing brackets.
  2. Step 2: Check syntax correctness

    =>, =, and >== are invalid operators in R.
  3. Final Answer:

    v[v >= 5] -> Option B
  4. Quick Check:

    Use >= for greater or equal in filtering [OK]
Hint: Use >= for greater or equal comparisons [OK]
Common Mistakes:
  • Using => which is invalid
  • Using = instead of comparison operator
  • Combining > and == incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard