0
0
R Programmingprogramming~10 mins

Comparison operators in R Programming - Interactive Code Practice

Choose your learning style9 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.