Bird
Raised Fist0
R Programmingprogramming~10 mins

Inline R code 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 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.

Practice

(1/5)
1. What is the purpose of using inline R code with `r expression` in documents?
easy
A. To comment out code in R scripts
B. To insert the result of an R expression directly into the text
C. To create a new R function
D. To define a variable in R

Solution

  1. Step 1: Understand inline R code usage

    Inline R code uses backticks and r to embed R expressions inside text.
  2. Step 2: Identify the purpose

    This allows the result of the R expression to appear directly in the document text, making it dynamic.
  3. Final Answer:

    To insert the result of an R expression directly into the text -> Option B
  4. Quick Check:

    Inline R code = insert R result in text [OK]
Hint: Inline code shows R results inside text using backticks [OK]
Common Mistakes:
  • Thinking inline code defines variables
  • Confusing inline code with comments
  • Believing inline code creates functions
2. Which of the following is the correct syntax for inline R code to show the sum of 2 and 3?
easy
A. `r 2 + 3`
B. `r sum(2 + 3`
C. `r sum 2, 3`
D. `r sum(2,3`

Solution

  1. Step 1: Inline R code evaluates expressions

    Inline R code executes the R expression between the backticks and inserts its result (here, 2+3=5).
  2. Step 2: Check each option's validity

    `r sum 2, 3` missing parens around args: sum 2 -> sum(2)=2, then ,3 -> 3 (wrong).
    `r sum(2 + 3` missing ) -> syntax error.
    `r 2 + 3` -> 5 (correct).
    `r sum(2,3` missing ) -> syntax error.
  3. Final Answer:

    `r 2 + 3` -> Option A
  4. Quick Check:

    Simple expression inside `r` is best [OK]
Hint: Use simple expressions inside `r` for inline code [OK]
Common Mistakes:
  • Adding extra parentheses inside sum()
  • Using incorrect commas or spaces
  • Confusing function call syntax
3. What will be the output of this inline R code snippet in a document? `r paste('Hello', 'World')`
medium
A. Error
B. paste('Hello', 'World')
C. HelloWorld
D. Hello World

Solution

  1. Step 1: Understand paste() function

    The paste() function joins strings with a space by default, so paste('Hello', 'World') returns "Hello World".
  2. Step 2: Inline code outputs the result

    The inline R code evaluates the expression and inserts the result "Hello World" into the document.
  3. Final Answer:

    Hello World -> Option D
  4. Quick Check:

    paste() joins strings with space [OK]
Hint: paste() joins words with space by default [OK]
Common Mistakes:
  • Expecting no space between words
  • Thinking inline code shows code, not result
  • Confusing paste() with paste0()
4. Identify the error in this inline R code: `r mean(c(1, 2, '3'))`
medium
A. Missing closing backtick
B. mean() cannot take vectors
C. Character '3' causes mean() to fail
D. No error, code runs fine

Solution

  1. Step 1: Check vector contents

    The vector c(1, 2, '3') mixes numbers and a character, so R converts all to character.
  2. Step 2: mean() expects numeric input

    mean() cannot calculate mean of characters, so it will produce an error.
  3. Final Answer:

    Character '3' causes mean() to fail -> Option C
  4. Quick Check:

    mean() needs numeric vector [OK]
Hint: mean() needs numeric values, no characters [OK]
Common Mistakes:
  • Ignoring type conversion in vectors
  • Assuming mean() works on mixed types
  • Missing quotes around characters
5. You want to display the number of rows in a data frame df inline in your report. Which inline R code correctly shows this count?
hard
A. `r nrow(df)`
B. `r length(df)`
C. `r df$nrow`
D. `r count(df)`

Solution

  1. Step 1: Recall function to get row count

    The function nrow() returns the number of rows in a data frame.
  2. Step 2: Check each option

    `r nrow(df)` uses nrow(df) which is correct. `r length(df)` uses length(df) which returns number of columns, not rows. `r df$nrow` tries to access a non-existent column nrow. `r count(df)` uses count() which is not a base R function for this purpose.
  3. Final Answer:

    `r nrow(df)` -> Option A
  4. Quick Check:

    nrow() gives row count [OK]
Hint: Use nrow() to get rows count inline [OK]
Common Mistakes:
  • Using length() for rows instead of columns
  • Trying to access nrow as a column
  • Using non-base functions without loading packages
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