What is the purpose of the which() function in the context of logical vectors in R?

  • It returns the indices of the elements that are TRUE
  • It returns the count of the elements that are TRUE
  • It returns the logical complement of the input vector
  • It returns the values of the elements that are TRUE
In the context of logical vectors in R, the which() function is used to return the indices of the elements that are TRUE. For example, which(c(TRUE, FALSE, TRUE)) would return the indices 1 and 3.

Describe a situation where you had to write a complex function in R. What were some of the challenges you faced, and how did you overcome them?

  • Handling large datasets efficiently
  • Implementing complex algorithms
  • Dealing with nested structures
  • All of the above
One situation where you might have to write a complex function in R is when handling large datasets, implementing complex algorithms, or dealing with nested structures such as lists of lists or data frames with multiple levels. Challenges may include optimizing performance, managing memory usage, handling edge cases, and ensuring code readability and maintainability. To overcome these challenges, you can use techniques like vectorization, efficient data structures, testing and debugging, and breaking down the problem into smaller, manageable components.

The ______ function in R can be used to handle missing values when calculating the mean.

  • mean()
  • na.rm()
  • na.omit()
  • na.mean()
The na.rm = TRUE parameter is used with the mean() function in R to handle missing values when calculating the mean. Setting na.rm = TRUE instructs R to ignore missing values in the calculation.

How do you perform multiplication in R?

  • *
  • +
  • -
  • /
In R, the operator * is used to perform multiplication. For example, 2 * 3 would result in 6.

Suppose you're asked to create a logical vector in R and perform some basic logical operations on it. How would you do it?

  • Use the c() function to create the vector and apply logical operations using the appropriate symbols and operands
  • Use the str_detect() function from the stringr package to create the vector and perform logical operations
  • Use the subset() function to create the vector and perform logical operations
  • Use the ifelse() function to create the vector and perform logical operations
To create a logical vector in R, you can use the c() function to combine logical values. For example, my_vector <- c(TRUE, FALSE, TRUE). Then, you can perform basic logical operations on the vector using the appropriate symbols and operands.

Does the median function in R handle missing values?

  • Yes, the median() function automatically ignores missing values
  • No, missing values cause an error in the median() function
  • Yes, but missing values are treated as 0 in the median calculation
  • Yes, but missing values need to be explicitly removed before using the median() function
Yes, the median() function in R automatically handles missing values by ignoring them in the calculation. It computes the median based on the available non-missing values in the vector or column.

Imagine you're debugging a piece of R code that uses nested functions and encountering unexpected behavior. What are some strategies you could use to identify the problem?

  • Use print statements or the browser() function to inspect intermediate results
  • Step through the code using a debugger
  • Check the input data and ensure it meets the expected format
  • All of the above
When debugging a piece of R code that uses nested functions and encountering unexpected behavior, you can use strategies such as using print statements or the browser() function to inspect intermediate results, stepping through the code using a debugger, and checking the input data to ensure it meets the expected format. These strategies help in identifying potential issues or discrepancies in the code and allow for thorough debugging and troubleshooting.

If a vector in R is created with elements of different data types, R will coerce the elements to the most flexible type, which is ______.

  • character
  • numeric
  • logical
  • integer
If a vector in R is created with elements of different data types, R will coerce the elements to the most flexible type, which is the character data type. The character type is considered the most flexible because it can represent other types by converting them to strings.

To fit a linear regression model in R, you would use the ______ function.

  • lm()
  • regmodel()
  • linreg()
  • regression()
To fit a linear regression model in R, you would use the lm() function. The lm() function stands for "linear model" and is used for estimating the coefficients of a linear regression model based on the given data.

The ______ function in R can be used to add text annotations to a plot.

  • text()
  • annotate()
  • label()
  • add_text()
The text() function in R can be used to add text annotations to a plot. It allows you to specify the coordinates and the text to be displayed at those coordinates, providing additional information or labels within the plot.