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.
The ifelse() function in R has the syntax ifelse(condition, ________, ________).
- value_if_true and value_if_false
- code_if_true and code_if_false
- result_if_true and result_if_false
- condition_if_true and condition_if_false
The ifelse() function in R has the syntax ifelse(condition, value_if_true, value_if_false). It evaluates the condition and returns the value_if_true if the condition is true, and the value_if_false if the condition is false. This function allows for vectorized conditional operations.
R's memory management can be inefficient as it stores all data in _________, which might be an issue with larger datasets.
- Cache
- Hard Disk
- RAM
- Registers
R stores all data in RAM, and as such, it might struggle with large datasets. This can sometimes limit its speed and efficiency, particularly in a data-intensive environment. However, there are packages and strategies to manage and overcome this limitation.
What is the primary use case for nested functions in R?
- Encapsulating helper functions within a larger function
- Reducing code duplication and improving modularity
- Implementing complex algorithms or workflows
- All of the above
The primary use case for nested functions in R is to encapsulate helper functions within a larger function. Nested functions can help in reducing code duplication, improving code modularity, and organizing related functionality together. They are especially useful when implementing complex algorithms or workflows that require multiple steps or subroutines.
What is the difference between "==" and "=" in R?
- "=" is not used in R
- "==" is used for assignment and "=" is used for comparison
- "==" is used for comparison and "=" is used for assignment
- There is no difference
In R, "==" is a comparison operator used to test for equality, while "=" is used for assignment, especially in the context of function arguments. However, "<-" is more commonly used for assignment.