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.

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.

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

  • colMeans()
  • rowMeans()
  • mean()
  • apply()
To calculate the mean of each column in a data frame in R, you would use the colMeans() function. The colMeans() function computes the mean values across each column of the data frame.

You have a script that isn't running as expected, and you suspect there's an issue with the syntax.

  • Ask someone else to fix it
  • Delete the script and start over
  • Ignore the error and continue
  • Use the traceback() function
The 'traceback()' function in R prints out the function call stack after an error occurs. This can help identify where the error is in the code, especially for syntax errors. Other debugging tools in R include 'debug()', 'browser()', and 'recover()'.

Suppose you're working with a list of vectors of different types and you need to concatenate them into a single vector. How would you approach this?

  • None of the above
  • Use the c() function
  • Use the paste() function
  • Use the unlist() function
If you're working with a list of vectors of different types and you need to concatenate them into a single vector, you can use the 'unlist()' function. This function can flatten a list of vectors into a single vector.

How does R handle arrays that contain elements of different data types?

  • R coerces the elements to the most flexible type within the array
  • R assigns each element a unique data type within the array
  • R throws an error if an array contains elements of different data types
  • None of the above
When an array is created in R with elements of different data types, R coerces the elements to the most flexible type within the array. This means that if the array contains elements of different data types, R will automatically convert them to a common type that can accommodate all the values in the array.

What are the potential risks or downsides of using recursive functions in R?

  • Excessive memory usage due to function call stack
  • Potential infinite recursion leading to stack overflow
  • Difficulty in understanding and debugging recursive code
  • All of the above
Some potential risks or downsides of using recursive functions in R include excessive memory usage due to the function call stack, the potential for infinite recursion leading to a stack overflow error, and the difficulty in understanding and debugging recursive code compared to iterative approaches. It is important to carefully design and test recursive functions to ensure they terminate correctly and efficiently handle the problem at hand.

Imagine you want to calculate the square root of a number in R. What would the syntax look like?

  • number^2
  • sqrt = number
  • sqrt(number)
  • square_root(number)
To calculate the square root of a number in R, we use the sqrt() function. For example, sqrt(4) would return 2.

Imagine you have two logical vectors and you need to perform an element-wise 'AND' operation. What would the syntax look like?

  • a && b
  • a && b
  • a & b
  • a and b
In R, the syntax for performing an element-wise 'AND' operation between two logical vectors is a & b. For example, if a and b are logical vectors, a & b would return a vector where each element is the result of the 'AND' operation between the corresponding elements of a and b.

How would you handle missing values when calculating the mean in R?

  • Use the na.rm = TRUE parameter in the mean() function
  • Replace missing values with 0 before using the mean() function
  • Exclude missing values from the vector before using the mean() function
  • All of the above
When calculating the mean in R, you can handle missing values by using the na.rm = TRUE parameter in the mean() function. Setting na.rm = TRUE instructs R to ignore missing values and compute the mean based on the available non-missing values. This ensures that missing values do not impact the calculation.