The ______ function in R can be used to apply a function to the margins of an array.
- apply()
- lapply()
- sapply()
- tapply()
The apply() function in R can be used to apply a function to the margins of an array. The margins refer to the dimensions of the array, such as rows or columns. By specifying the margin argument in the apply() function, you can apply a function to the rows or columns of an array and obtain the results in a desired format.
Can you describe a scenario where you used logical vectors in R for subsetting data?
- Subsetting a dataset based on a certain condition or criteria
- Creating logical conditions for applying specific transformations
- Filtering out missing values in a dataset
- All of the above
A scenario where logical vectors are used in R for subsetting data is when you want to extract specific rows from a dataset based on a certain condition or criteria. For example, you can use a logical vector to subset a dataset to include only rows where a certain variable meets a specific condition.
To check multiple conditions in an if statement in R, you can use the ________ or ________ operators.
- & and
- | and
- ! and
- %in% and
To check multiple conditions in an if statement in R, you can use the & operator for logical 'AND' and the | operator for logical 'OR'. For example, if (condition1 & condition2) { code to execute } will check if both condition1 and condition2 are true.
Can you describe a scenario where you would need to use a global variable in R?
- Storing program configuration settings
- Sharing data between multiple functions
- Implementing a global counter or identifier
- All of the above
There are various scenarios where you might need to use a global variable in R. For example, when storing program configuration settings that need to be accessed by multiple functions, sharing data between multiple functions or code blocks, or implementing a global counter or identifier to keep track of certain program states. Global variables can be useful in these cases to facilitate communication and data sharing across different parts of the program.
In R, the ______ function can be used to list all the variables in the global environment.
- ls()
- vars()
- objects()
- globals()
In R, the ls() function can be used to list all the variables in the global environment. It returns the names of all the objects or variables defined in the global environment, allowing you to inspect and access the global variables present in your program.
Can you explain how R handles 'AND' and 'OR' operations with NA values?
- In 'AND' operations, if either operand is 'NA', the result is 'NA'. In 'OR' operations, if either operand is 'NA', the result is 'NA'.
- In 'AND' operations, if either operand is 'NA', the result is 'FALSE'. In 'OR' operations, if either operand is 'NA', the result is 'TRUE'.
- In 'AND' operations, if either operand is 'NA', the result is 'TRUE'. In 'OR' operations, if either operand is 'NA', the result is 'FALSE'.
- In 'AND' operations, if either operand is 'NA', an error is thrown. In 'OR' operations, if either operand is 'NA', an error is thrown.
When performing 'AND' and 'OR' operations in R, if either operand is 'NA', the result will be 'NA' for both 'AND' and 'OR' operations. This is because the presence of 'NA' indicates that the value is missing or unknown, resulting in an unknown outcome for the logical operation.
How would you customize the appearance of an R scatter plot, including changing colors, markers, and sizes?
- By using the col, pch, and cex parameters in the plot() function
- By using the legend() function
- By using the theme() function from the ggplot2 package
- By using the par() function and graphical parameters
To customize the appearance of an R scatter plot, including changing colors, markers, and sizes, you can use the col parameter to change colors, the pch parameter to change markers, and the cex parameter to change the size of the points. These graphical parameters can be specified within the plot() function.
How can you handle situations where your calculations result in 'Inf' or 'NaN'?
- Both of these methods
- None of the above
- Use ifelse() function to handle such situations
- Use is.finite() function to check the result
One way to handle this is by using the is.finite() function which checks whether the value is finite or not. This function returns FALSE if the value is Inf or NaN and TRUE otherwise. Depending on the use case, you can then decide how to handle these non-finite values.
To calculate the median of each column in a data frame in R, you would use the ______ function.
- apply()
- colMedian()
- median()
- colMeans()
To calculate the median of each column in a data frame in R, you would use the apply() function. By specifying the appropriate margin argument (2 for columns), you can apply the median() function across each column of the data frame.
In R, to access the first element of an array named myarray, you would use ______.
- myarray[1]
- myarray[[1]]
- myarray[1, 1]
- myarray[[1, 1]]
In R, to access the first element of an array named myarray, you would use myarray[1]. The square brackets [] are used to extract elements from an array. The index 1 refers to the first element of the array.