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.

Suppose you're asked to create a pie chart in R that shows the distribution of a categorical variable in a data set. How would you do it?

  • Use the pie() function and provide a vector of non-negative numeric values representing the proportions of the segments
  • Use the barplot() function and specify the categorical variable as the x argument
  • Use the plot() function with type = "pie" and specify the categorical variable as the data argument
  • Use the ggplot2 package and the geom_bar() function with the categorical variable as the x aesthetic
To create a pie chart in R that shows the distribution of a categorical variable in a data set, you would use the pie() function. Provide a vector of non-negative numeric values representing the proportions of the segments corresponding to each category.

If you want to include a literal backtick in a string in R, you would use the escape sequence ________.

  • `
  • t
  • b
  • '
In R, to include a literal backtick in a string, you would use the escape sequence ' . For example, "This is a backtick: '" would result in the string This is a backtick: `.

The ______ function in R can be used to find the index of the maximum value in a vector.

  • which.max()
  • which.min()
  • index.max()
  • index.min()
The which.max() function in R can be used to find the index of the maximum value in a vector. The which.max() function returns the index of the first occurrence of the maximum value.

What are the rules for naming variables in R?

  • Can start with a number, Can contain special characters, Case-insensitive
  • Can start with a number, Cannot contain special characters, Case-insensitive
  • Cannot start with a number, Can contain special characters, Case-sensitive
  • Cannot start with a number, Cannot contain special characters, Case-sensitive
Variables in R should start with a letter, and can contain letters, numbers, period (.) and underscore (_). Variable names in R are also case sensitive, so "mydata" and "MyData" would refer to different variables.

Suppose you're writing a function in R that simulates a random process many times. How would you use a for loop to accomplish this?

  • for (i in 1:n_simulations) { simulate_one_iteration() }
  • for (i in 1:n_simulations) { simulate(i) }
  • simulate(n_simulations)
  • for (simulation in n_simulations) { simulate_one_iteration() }
To simulate a random process many times using a for loop, you can iterate from 1 to the desired number of simulations (n_simulations), and within each iteration, call a function simulate_one_iteration() that performs one simulation of the random process. This allows you to repeat the simulation process the desired number of times.

Can you nest if statements in R?

  • Yes, if statements can be nested inside other if or if-else statements
  • No, R does not allow nested if statements
  • Yes, but only one level of nesting is allowed in R
  • Yes, but the nesting level is limited to two levels in R
Yes, if statements can be nested inside other if or if-else statements in R. This allows for more complex conditional logic by evaluating multiple conditions within a single if statement.

How do you create a vector in R?

  • array() function
  • list() function
  • matrix() function
  • vector() function
In R, vectors are created using the "vector()" function or the "c()" function, which is more commonly used. For example, c(1, 2, 3) creates a numeric vector with elements 1, 2, and 3.

What function is commonly used to view the structure of a data frame in R?

  • str()
  • summary()
  • view()
  • head()
The str() function is commonly used to view the structure of a data frame in R. The str() function displays a concise summary of the structure of the data frame, including the variable names, data types, and a preview of the data.