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.

Imagine you need to create a predictive model in R. Can you walk me through the steps you would take and the packages you might use?

  • Buy more computing power, Choose any model, Ignore understanding the problem
  • None of the above
  • Start by choosing a model, Ignore data preprocessing, Use any package randomly
  • Understand the problem, Preprocess the data, Choose an appropriate model, Use caret or equivalent package for modeling
The first step in building a predictive model should be understanding the problem at hand and the data available. Preprocessing of the data would come next. Then, choosing an appropriate model based on the problem and data is crucial. The caret package in R provides functions to streamline the model building process.

What is a global variable in R?

  • A variable defined outside of any function that can be accessed from anywhere in the program
  • A variable defined inside a function that can be accessed only within that function
  • A variable defined in the global environment that is read-only
  • A variable that is used for global configuration settings in R
A global variable in R is a variable that is defined outside of any function and can be accessed from anywhere in the program. It is stored in the global environment and is accessible to all functions and code segments within the R session. Global variables can hold data or configuration settings that need to be accessed and modified across multiple functions or code blocks.

What function in R is used to calculate the square root of a number?

  • root()
  • sqr()
  • sqrt()
  • squareroot()
The sqrt() function in R is used to calculate the square root of a number. For example, sqrt(4) would return 2.

How would you calculate a running median in R?

  • Use the runMedian() function from the caTools package
  • Use the rollapply() function from the zoo package
  • Use the cummedian() function from the stats package
  • Use the median() function with a sliding window approach
To calculate a running median in R, you can use the rollapply() function from the zoo package. The rollapply() function allows you to specify a window size and apply a function (such as median()) to a rolling window of values. This can be useful for analyzing time series or other sequential data.

The ________ function in R is used to split a string into components based on a specific character or string.

  • divide()
  • separate()
  • split()
  • strsplit()
The strsplit() function in R is used to split a string into components based on a specific character or string. For example, strsplit("Hello World", " ") would return a list with two components: "Hello" and "World".

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

  • legend()
  • key()
  • colorbar()
  • annotate()
The legend() function in R can be used to add a legend to a plot. It allows you to specify the labels and colors corresponding to different elements in the plot, making it easier to interpret the plot.