0
0
R Programmingprogramming~10 mins

Inline R code 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 print the value of x inline.

R Programming
x <- 5
cat("The value of x is ", [1], "\n")
Drag options to blanks, or click blank then click option'
Ax
B"x"
Cprint(x)
Dvalue(x)
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around x prints the letter 'x' instead of its value.
Using print(x) inside cat() will not work as expected.
2fill in blank
medium

Complete the code to embed the result of 3 + 4 inline in the string.

R Programming
cat(sprintf("3 + 4 equals %d\n", [1]))
Drag options to blanks, or click blank then click option'
A"3 + 4"
Bsum(3, 4)
C3 + 4
D"7"
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the expression inside quotes prints it literally.
Using sum(3, 4) is valid but not the simplest inline expression here.
3fill in blank
hard

Fix the error in the code to correctly embed the variable y inline.

R Programming
y <- 10
cat(paste0("Value of y is ", [1]))
Drag options to blanks, or click blank then click option'
Aas.character(y)
By
Cprint(y)
D"y"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "y" prints the letter 'y' instead of its value.
Using print(y) returns NULL inside paste0.
4fill in blank
hard

Fill both blanks to create a named list with word lengths greater than 3.

R Programming
words <- c("cat", "house", "dog", "elephant")
lengths <- list()
for (word in words) {
  if (nchar(word) [1] 3) {
    lengths[[word]] <- [2]
  }
}
Drag options to blanks, or click blank then click option'
A>
Bnchar(word)
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' reverses the condition.
Storing the word instead of its length.
5fill in blank
hard

Fill all three blanks to create a named vector with uppercase keys and values for words longer than 4.

R Programming
words <- c("apple", "bat", "carrot", "dog")
result <- c()
for (w in words) {
  if (nchar(w) [1] 4) {
    result[[2]] <- [3]
  }
}
Drag options to blanks, or click blank then click option'
A>
Btoupper(w)
Cw
Dtolower(w)
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' reverses the condition.
Using lowercase for keys instead of uppercase.
Assigning the key and value incorrectly.