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.
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.
In R, if a variable is not found in the local environment of a function, the function will look in the ______ environment.
- Global
- Parent
- Child
- Calling
In R, if a variable is not found in the local environment of a function, the function will look in the parent environment. This allows functions to access variables defined in higher-level environments, such as variables defined outside the function but within the parent environment.