Imagine you have a string in R and you want to convert it to uppercase. How would you do this?

  • Use the to_upper() function
  • Use the toupper() function
  • Use the upper() function
  • Use the uppercase() function
In R, the toupper() function is used to convert a string to uppercase. For example, toupper("Hello") would return "HELLO".

Can you discuss the advantages and disadvantages of base R plotting versus ggplot2?

  • Base R plotting is more flexible, but ggplot2 provides a more structured grammar of graphics
  • Base R plotting has a steeper learning curve, but ggplot2 is easier to learn
  • Base R plotting is faster, but ggplot2 produces more visually appealing plots
  • Base R plotting has limited plotting options, but ggplot2 is highly customizable
Base R plotting offers more flexibility, allowing for a wider range of customization and plot types. However, ggplot2 provides a more structured and consistent grammar of graphics, making it easier to create complex plots. The choice between the two often depends on personal preference and the specific requirements of the plot.

Imagine you need to determine the data type of a variable in R. How would you do this?

  • Use the mode() function on the variable
  • Use the typeof() function on the variable
  • Use the class() function on the variable
  • Use the str() function on the variable
To determine the data type of a variable in R, you would use the typeof() function on the variable. The typeof() function returns a character string representing the data type of the object.

Imagine you want to print a user-friendly message based on the value of a variable in R. How would you approach it?

  • None of the above
  • Use the cat() function with switch statements
  • Use the echo() function with for loops
  • Use the print() function with if-else statements
You can use conditional statements like if-else in combination with the print() function to display different messages based on the value of a variable. This allows you to make the output more user-friendly and informative.

Can you discuss how R handles missing values in statistical calculations?

  • R provides functions to handle missing values, such as na.rm = TRUE in calculations
  • R automatically excludes missing values in statistical calculations
  • R treats missing values as 0 in calculations
  • R displays an error when encountering missing values
R handles missing values in statistical calculations by providing functions that allow for exclusion of missing values from calculations. For example, many statistical functions in R have an na.rm parameter that, when set to TRUE, ignores missing values and performs calculations on the available data. This allows for accurate statistical analyses even when data contain missing values.

Suppose you're asked to write a recursive function in R that calculates the factorial of a number. How would you do it?

  • factorial <- function(n) { if (n <= 1) { return(1) } else { return(n * factorial(n - 1)) } }
  • factorial <- function(n) { if (n <= 1) { return(n) } else { return(n + factorial(n - 1)) } }
  • factorial <- function(n) { if (n <= 1) { return(0) } else { return(n * factorial(n)) } }
  • All of the above
To write a recursive function in R that calculates the factorial of a number, you can use the following code: factorial <- function(n) { if (n <= 1) { return(1) } else { return(n * factorial(n - 1)) } }. The function checks if the input n is less than or equal to 1. If it is, it returns 1 (base case). Otherwise, it multiplies n with the factorial of n - 1 (recursive case). This recursive calculation continues until the base case is reached.

Can you describe a scenario where you need to include double quotes within a string in R?

  • When writing a string that represents a quote or dialogue
  • When including a variable value inside a string
  • When representing HTML or XML code within a string
  • All of the above
Including double quotes within a string in R is commonly required when writing a string that represents a quote or dialogue. For example, "She said, "Hello!"" represents the string She said, "Hello!".

How can you use vectorization in R to avoid the need for if-else statements?

  • By applying functions or operations directly to vectors or data frames
  • By using the ifelse() function for vectorized conditional operations
  • By using the apply family of functions to iterate over vectors or data frames
  • All of the above
Vectorization in R allows you to apply functions or operations directly to vectors or data frames, which eliminates the need for explicit if-else statements. The ifelse() function is specifically designed for vectorized conditional operations, providing a concise and efficient alternative to if-else statements when working with vectors or data frames.

To calculate the median of each column in a data frame in R, you would use the ______ function.

  • apply()
  • colMedian()
  • median()
  • colMeans()
To calculate the median of each column in a data frame in R, you would use the apply() function. By specifying the appropriate margin argument (2 for columns), you can apply the median() function across each column of the data frame.

How can you handle situations where your calculations result in 'Inf' or 'NaN'?

  • Both of these methods
  • None of the above
  • Use ifelse() function to handle such situations
  • Use is.finite() function to check the result
One way to handle this is by using the is.finite() function which checks whether the value is finite or not. This function returns FALSE if the value is Inf or NaN and TRUE otherwise. Depending on the use case, you can then decide how to handle these non-finite values.

How would you customize the appearance of an R scatter plot, including changing colors, markers, and sizes?

  • By using the col, pch, and cex parameters in the plot() function
  • By using the legend() function
  • By using the theme() function from the ggplot2 package
  • By using the par() function and graphical parameters
To customize the appearance of an R scatter plot, including changing colors, markers, and sizes, you can use the col parameter to change colors, the pch parameter to change markers, and the cex parameter to change the size of the points. These graphical parameters can be specified within the plot() function.

Can you explain how R handles 'AND' and 'OR' operations with NA values?

  • In 'AND' operations, if either operand is 'NA', the result is 'NA'. In 'OR' operations, if either operand is 'NA', the result is 'NA'.
  • In 'AND' operations, if either operand is 'NA', the result is 'FALSE'. In 'OR' operations, if either operand is 'NA', the result is 'TRUE'.
  • In 'AND' operations, if either operand is 'NA', the result is 'TRUE'. In 'OR' operations, if either operand is 'NA', the result is 'FALSE'.
  • In 'AND' operations, if either operand is 'NA', an error is thrown. In 'OR' operations, if either operand is 'NA', an error is thrown.
When performing 'AND' and 'OR' operations in R, if either operand is 'NA', the result will be 'NA' for both 'AND' and 'OR' operations. This is because the presence of 'NA' indicates that the value is missing or unknown, resulting in an unknown outcome for the logical operation.