How would you customize the appearance of an R plot, including changing colors, labels, and legend?
- By using the col, xlab, ylab parameters in plot()
- 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 plot, including changing colors, labels, and legends, you can use the par() function along with various graphical parameters. These parameters allow you to control aspects such as colors, labels, axes, and more.
Imagine you need to create a global variable within a function in R. How would you do this?
- Define the variable using the <<- operator inside the function
- Define the variable using the -> operator inside the function
- Define the variable using the = operator inside the function
- It is not possible to create a global variable within a function
To create a global variable within a function in R, you can use the <<- operator. By assigning a value to a variable using <<- inside a function, the variable becomes a global variable that can be accessed from anywhere in the program. However, it is generally recommended to limit the use of global variables within functions for better code organization and modularity.
In R, the ! symbol represents the logical ________ operation.
- AND
- NOT
- OR
- XOR
In R, the ! symbol represents the logical NOT operation. It is used to negate the logical value of an expression. For example, !TRUE would return FALSE.
The ________ function in R can be used to write output into a file.
- echo()
- print()
- save()
- write()
The write() function in R is typically used to write data to a file. It can write a single R object (like a vector, matrix, or data frame) to a text file, with elements separated by a specified delimiter.
The ______ function in R can be used to handle missing values when calculating the median.
- median()
- na.rm()
- na.omit()
- na.median()
The na.rm = TRUE parameter is used with the median() function in R to handle missing values when calculating the median. Setting na.rm = TRUE instructs R to ignore missing values in the calculation.
If a list in R is created with elements of different data types, R will ______.
- coerce the elements to the most flexible type
- retain the individual data types of the elements
- throw an error
- None of the above
If a list in R is created with elements of different data types, R will retain the individual data types of the elements within the list. Unlike vectors, where elements are coerced to a common type, lists allow for heterogeneity and preserve the specific data types of each element.
Suppose you're asked to write a function in R that calculates the average of a vector of numbers. How would you do it?
- average <- function(x) { sum(x) / length(x) }
- average <- function(x) { mean(x) }
- average <- function(x) { total <- 0; for (num in x) { total <- total + num }; total / length(x) }
- All of the above
To write a function in R that calculates the average of a vector of numbers, you can use the following code: average <- function(x) { sum(x) / length(x) }. The function takes a vector x as input, calculates the sum of the elements in x, divides it by the length of x, and returns the average value.
If a data frame in R is created with columns of different data types, R will ______.
- Assign the most common data type to all columns
- Raise an error
- Assign the data type based on the first column
- Treat each column independently with its own data type
If a data frame in R is created with columns of different data types, R will treat each column independently with its own data type. This flexibility allows for efficient handling and analysis of heterogeneous data.
How would you perform a linear regression analysis in R?
- Use the lm() function
- Use the regression() function
- Use the linreg() function
- Use the regmodel() function
To perform a linear regression analysis in R, you would use the lm() function. The lm() function fits a linear regression model to the data, estimating the coefficients and providing various statistical measures such as p-values and R-squared.
Imagine you need to create a vector in R containing the first 100 positive integers. How would you do this?
- Use the : operator to create a sequence from 1 to 100
- Use the seq() function with the from and to arguments
- Use the rep() function to repeat the number 1, 100 times
- Use the sample() function to randomly select numbers from 1 to 100
To create a vector in R containing the first 100 positive integers, you can use the : operator to create a sequence from 1 to 100. The : operator generates a sequence of consecutive integers between two given endpoints. In this case, it will create a sequence from 1 to 100.