Suppose you're asked to create a scatter plot in R that requires transformation or normalization of the variables. How would you approach this task?

  • Transform or normalize the variables before creating the scatter plot
  • Create the scatter plot and then apply transformation or normalization to the plot
  • Use specialized functions or packages for transformation or normalization within the scatter plot function
  • Both A and C
To create a scatter plot in R that requires transformation or normalization of the variables, it is recommended to transform or normalize the variables before creating the scatter plot. This ensures that the relationship between the variables is accurately represented in the plot. Specialized functions or packages can be used for the transformation or normalization process.

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

  • R coerces the elements to the most flexible type
  • R throws an error if a vector contains elements of different data types
  • R automatically converts the elements to a common type based on their values
  • R assigns each element a unique data type within the vector
When a vector in R contains elements of different data types, R coerces the elements to the most flexible type among them. This flexibility is determined by a hierarchy of types, where logical < integer < numeric < character. R will automatically convert the elements to a common type based on this hierarchy, ensuring consistency within the vector.

You're asked to print a sequence of numbers from 1 to 10 in R. How would you do it?

  • None of the above
  • print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  • print(1:10)
  • print([1,10])
In R, the colon operator ':' generates a sequence of numbers. So, to print a sequence of numbers from 1 to 10, the syntax would be print(1:10).

The ______ parameter in the scatter plot function in R can be used to change the size of the points.

  • col
  • pch
  • cex
  • marker
The cex parameter in the scatter plot function in R can be used to change the size of the points. It allows you to specify a numerical value that determines the relative size of the points on the scatter plot.

To calculate the median of a numeric vector in R, you would use the ______ function.

  • median()
  • mean()
  • sd()
  • var()
To calculate the median of a numeric vector in R, you would use the median() function. The median() function returns the middle value of a sorted vector or the average of the two middle values if the vector has an even number of values.

What is a nested function in R?

  • A function that is defined within another function
  • A function that calls another function
  • A function that takes another function as an argument
  • A function that returns another function
A nested function in R is a function that is defined within another function. It is created and exists within the scope of the outer function. The nested function can access variables from the outer function and can only be called from within the outer function.

Imagine you need to create a recursive function in R that computes the nth Fibonacci number. How would you do this?

  • fibonacci <- function(n) { if (n <= 1) { return(n) } else { return(fibonacci(n - 1) + fibonacci(n - 2)) } }
  • fibonacci <- function(n) { if (n <= 1) { return(0) } else { return(fibonacci(n) + fibonacci(n - 1)) } }
  • fibonacci <- function(n) { if (n <= 1) { return(1) } else { return(fibonacci(n + 1) + fibonacci(n - 1)) } }
  • All of the above
To create a recursive function in R that computes the nth Fibonacci number, you can use the following code: fibonacci <- function(n) { if (n <= 1) { return(n) } else { return(fibonacci(n - 1) + fibonacci(n - 2)) } }. The function checks if the input n is less than or equal to 1. If it is, it returns n (base case). Otherwise, it recursively calls itself to calculate the Fibonacci number by summing the two previous Fibonacci numbers.

Suppose you're asked to write a function in R that takes an array of numbers and returns a new array with each element squared. How would you do it?

  • Use a nested for loop to iterate over each element and calculate the square
  • Use the apply() function with a custom function to calculate the square of each element
  • Use the ^ operator to raise the array to the power of 2
  • Use the sqrt() function to calculate the square root of each element
To write a function in R that takes an array of numbers and returns a new array with each element squared, you can use a nested for loop to iterate over each element of the array and calculate the square. By storing the squared values in a new array, you can return the resulting array as the output of the function.

Suppose you're asked to write a for loop in R that prints the squares of the numbers 1 to 10. How would you do it?

  • for (i in 1:10) { print(i^2) }
  • for (i in 1:10) { print(i * i) }
  • for (i in 1:10) { print(square(i)) }
  • for (i in 1:10) { print(pow(i, 2)) }
To print the squares of the numbers 1 to 10, you can use the for loop for (i in 1:10) { print(i^2) }. It iterates through the values 1 to 10, calculates the square of each value, and prints the result.

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

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