Knowledge Quiz: Functions

Note
  • Click the check-mark button to check your answer.
  • Click the question-mark button to see an explanation of the solution.

Which of the following options represent R function calls?

You can recognize a valid function call by the pattern: FUNCTION_NAME().

  • This is a length-one numeric vector.
  • This is a symbol (i.e., the name of an object).
  • Correct. This function call will list the library paths where R will install packages on your system.
  • Correct. This function call would generate 100 random standard normal deviates.
  • Correct. This function call would calculate the trimmed mean of x, ignoring any missing values.
  • This is R’s infinite value code.

Which of the following function calls will correctly round the value 9.8765 to 2 decimal places?

  • Correct: both arguments are named, so their order doesn’t matter, even though it differs from the previous answer.
  • Correct: the arguments are supplied in the order round() expects (x, then digits), so no names are needed.
  • Incorrect: without argument names, R matches by position — the first value fills x and the second fills digits. This call rounds 2 (interpreted as x) to 9.8765 decimal places, which is a nonsensical number of digits, not the intended calculation.
  • Incorrect: round() expects an argument named digits, not digit. R will not recognize this argument name.
  • Correct: both arguments are named, so their order doesn’t matter.

Which of the following lines of R code will produce an error when run?

  • Correct: R is case-sensitive, and there is no function called Sum() (only sum()), so this errors.
  • Correct: round() has no argument called digit (it’s digits), so this errors.
  • Incorrect: this is valid, complete R code, so it runs without error.
  • Incorrect: this is valid, complete R code (digits is the correct argument name), so it runs without error.
  • Correct: the closing parenthesis is missing, so this errors.
Back to top