introduction to r programming for data science coursera answers week 3

Practice Quiz

1. Which looping statement should you use if you want to continue to perform an operation if the value of an expression is true?

  • For Each loop
  • For loop
  • Do While loop
  • While loop

2. What happens if a user-defined function is missing the return statement?

  • The function returns an error.
  • The function returns the last evaluated expression in the function.
  • The function returns a NULL value.
  • The function returns the first evaluated expression it encounters and then immediately exits the function.

3. Assume you have a variable called word_string that contains the string โ€œThe quick red fox jumped over the lazy dog.โ€ Which function can you use to replace the word โ€œdogโ€ with the word โ€œcatโ€ in the word_string variable?

  • nchar()
  • chartr()
  • strsplit()
  • substr()

4. Which of the following statements about regular expressions are true? Select two answers.

  • Regular expressions perform mathematical operations on numeric values.
  • Regular expressions are used to match patterns in strings and text.

  • Regular expressions evaluate a condition to determine if it is true or false.
  • Regular expressions are primarily used for data extraction.

5. How do you convert a UNIX date/time format to an R Date object?

  • Pass the UNIX date/time value to the as.Date() function.
  • Pass the UNIX date/time value to the as.POSIXct() function and then pass that value to the as.Date() function.
  • Pass the UNIX date/time value to the as.POSIXct() function and then pass that value to the is.Date() function.
  • Pass the UNIX date/time value to the is.Date() function.

6. What type of statement should you add to your code if you know that an error or warning might occur?

  • Incorrect distractor
  • A catchError statement.
  • A tryCatch statement.
  • A tryError statement.

Graded Quiz

7. What is the result of the conditional statement 25 > 15 | 99 >= 100?

  • TRUE
  • FALSE

8. How do you define a global variable in a function?

  • Use the == assignment operator.
  • Use the -> assignment operator.
  • Use the <- assignment operator.
  • Use the <<- assignment operator.

9. You can use the str_sub() function to form a substring by counting back from the last position. This function is part of which package?

  • stringr
  • tidyr
  • readr
  • purrr

10. Assume you have a data frame that contains a string variable called โ€˜phoneโ€™. The phone numbers in this variable appear in (###) ###-#### or ###-###-#### format. Which feature of R can you use to isolate the area code (the three numbers between the parentheses or the first three numbers)?

  • It is not possible to do this using R.
  • A string operation.
  • A mathematical operation.
  • A regular expression.

11. When you convert a date in string format to a Date object, what information do you need to pass to the as.Date() function? Select two answers.

  • The UNIX format of the string.
  • The number of days since January 1, 1970.
  • The date format of the string.

  • The string containing the date.

12. What is the difference between an error and a warning in you R code?

  • You can catch a warning, but you cannot catch an error.
  • An error halts code execution, while a warning does not.
  • A warning halts code execution, while an error does not.
  • You can catch an error, but you cannot catch a warning.

Leave a Reply