Suppose you're asked to write a function in R that takes a data frame and returns a new data frame with only numeric columns. How would you do it?

  • Use the select() function from the dplyr package
  • Use the filter() function from the dplyr package
  • Use the subset() function
  • Use the keep() function from the purrr package
To accomplish this task, you can use the select() function from the dplyr package in R. You can specify the column selection criteria to include only numeric columns and obtain a new data frame with the desired columns.

Suppose you're writing a function in R to handle a complex set of conditions. How would you approach this to avoid deep nesting of if statements?

  • Break down the conditions into smaller functions or helper variables
  • Utilize switch() or case_when() functions for handling multiple conditions
  • Use logical operators and vectorization for efficient conditional operations
  • All of the above
To avoid deep nesting of if statements when writing a function in R to handle complex conditions, you can employ various approaches. This includes breaking down the conditions into smaller functions or helper variables, utilizing functions like switch() or case_when() to handle multiple conditions concisely, and leveraging logical operators and vectorization for efficient conditional operations. These approaches help enhance code readability, maintainability, and performance.

The Recall() function in R is used to ________ within a function.

  • Call the function itself recursively
  • Access the parent environment
  • Return multiple values from the function
  • Stop the recursion
The Recall() function in R is used to call the function itself recursively from within the function. It is commonly used in recursive functions to simplify the syntax and improve readability when making recursive calls. By using Recall(), you can avoid explicitly writing the function name again, making the recursive calls more concise.

How does R internally represent dates and times?

  • As character values
  • As logical values
  • As numeric values
  • As special date/time objects
R internally represents dates and times as special date/time objects using classes such as 'Date', 'POSIXct', and 'POSIXlt'. These classes store date and time information in a format that can be easily manipulated in R.

Suppose you want to print the output of a function that calculates the square of a number. What would the syntax look like?

  • None of the above
  • print(sq(x))
  • print(square(x))
  • print(x^2)
First, you'd have to define a function that calculates the square of a number (e.g., square <- function(x) {return(x^2)}). Then you could use print(square(x)) to print the result of squaring a number. Note: 'x' should be replaced with the number you want to square.

Suppose you're asked to write an if-else statement in R that checks if a number is positive or negative. How would you do it?

  • if (number > 0) { code for positive number } else { code for negative number }
  • if (number >= 0) { code for positive number } else { code for negative number }
  • if (number == 0) { code for positive number } else { code for negative number }
  • if (number != 0) { code for positive number } else { code for negative number }
To write an if-else statement in R that checks if a number is positive or negative, you can use the condition if (number > 0) { code for positive number } else { code for negative number }. If the number is greater than 0, the code inside the if block will be executed; otherwise, the code inside the else block will be executed.

Can you discuss alternatives to using nested if statements in R?

  • Using the switch() function for handling multiple conditions
  • Utilizing the ifelse() function for vectorized conditional operations
  • Employing the case_when() function from the dplyr package
  • All of the above
Instead of using nested if statements, there are alternative approaches in R. These include using the switch() function for handling multiple conditions, utilizing the ifelse() function for vectorized conditional operations, and employing the case_when() function from the dplyr package for conditional operations in data frames. These alternatives can simplify code structure and enhance code readability.

Imagine you need to create a data frame in R containing the first 100 positive integers and their corresponding square values in two separate columns. How would you do this?

  • Using the data.frame() function
  • Using the matrix() function
  • Using the c() function
  • Using the seq() function
To create a data frame with the first 100 positive integers and their corresponding square values, you can use the data.frame() function. You can create two separate vectors, one for the integers and one for the squares, and then pass them as arguments to the data.frame() function to create the desired data frame.

The ______ function in R can be used to apply a function to each element of a vector or columns of a data frame.

  • apply()
  • map()
  • iterate()
  • process()
The apply() function in R can be used to apply a function to each element of a vector or columns of a data frame. The apply() function simplifies repetitive operations by iterating over the elements or columns and applying the specified function.

How would you calculate the mode of a factor in R?

  • Convert the factor to a character vector and use mode()
  • Apply the table() function to the factor
  • Use the levels() function on the factor
  • Apply the median() function to the factor
To calculate the mode of a factor in R, you can apply the table() function to the factor. The table() function counts the frequencies of each level in the factor, allowing you to identify the most frequent level as the mode.