R is a versatile language primarily used for statistical analysis and data visualization. With its extensive library support and flexibility, R has cemented its place as an essential tool for data scientists and researchers. Dive into this set of 25 multiple-choice questions to test and expand your R programming knowledge!
1. What is R primarily used for?
Answer:
Explanation:
While R can have other applications, its primary use is for statistics and data visualization.
2. Which function is used to install packages in R?
Answer:
Explanation:
install.packages() is used to install new packages in R. The other functions are used to load or check packages.
3. How do you load a package in R?
Answer:
Explanation:
require() is used to load a package in R. If the package isn't installed, it returns FALSE.
4. What is the correct way to load the ggplot2 package?
Answer:
Explanation:
Both require("ggplot2") and library(ggplot2) are correct methods to load the ggplot2 package in R.
5. Which data structure in R can hold elements of different types?
Answer:
Explanation:
Lists in R can hold elements of different types, while vectors, matrices, and arrays hold elements of the same type.
6. How do you create a vector c(1,2,3,4) in R?
Answer:
Explanation:
Both 1:4 and v = c(1,2,3,4) create the desired vector.
7. In R, what is the output of the expression c(1,2,3) + c(1,1,1)?
Answer:
Explanation:
Vector addition in R is element-wise, so 1+1=2, 2+1=3, and 3+1=4.
8. Which function is used to check if an R package is installed?
Answer:
Explanation:
While require() primarily loads a package, it returns FALSE if the package isn't installed, effectively checking its presence.
9. Which of the following is NOT a valid variable name in R?
Answer:
Explanation:
Variable names in R cannot start with a number.
10. Which base R function provides the structure of an R object?
Answer:
Explanation:
str() gives the structure of an R object, detailing the type, length, and content.
11. What is the default data visualization package in R?
Answer:
Explanation:
R's base graphics system is the default plotting system.
12. What does the apply() function do in R?
Answer:
Explanation:
apply() is used to apply a function across rows or columns of a matrix.
13. Which function is used to combine multiple vectors into a data frame?
Answer:
Explanation:
data.frame() is used to combine multiple vectors into a single data frame.
14. What is the output of class(NA) in R?
Answer:
Explanation:
By default, NA (representing missing data) is of the logical type.
15. Which R function is used for statistical hypothesis tests?
Answer:
Explanation:
t.test() is a commonly used function for t-tests, which are types of statistical hypothesis tests.
16. Which function in R is used to seed the random number generator to ensure reproducibility?
Answer:
Explanation:
set.seed() initializes the random number generator, ensuring consistent and reproducible results.
17. What does the rnorm() function do in R?
Answer:
Explanation:
rnorm() generates random numbers following a normal distribution.
18. How do you access the second element of a list named my_list?
Answer:
Explanation:
Lists in R are accessed using double square brackets.
19. Which of the following is true about R's data frames?
Answer:
Explanation:
In R's data frames, each row is an observation and each column is a variable or feature.
20. Which R function is used to handle exceptions?
Answer:
Explanation:
tryCatch() provides exception handling by capturing errors and allowing for recovery actions.
21. Which of the following R functions is used to get the class of an object?
Answer:
Explanation:
In R, both class() and typeof() provide information about the type or class of an object, but they can sometimes return different results based on the underlying data structure.
22. Which base R function returns a logical vector indicating if each element is missing?
Answer:
Explanation:
is.na() checks each element of an object and returns a logical vector indicating if the elements are NA (missing).
23. What does the lapply() function return?
Answer:
Explanation:
lapply() returns a list of the same length as the input. It applies a function to each element of a list or vector.
24. Which R function is used to export data frames to .csv files?
Answer:
Explanation:
write.csv() is the function used to write a data frame to a .csv file in R.
25. How do you subset rows of a data frame df where the column 'age' is greater than 30?
Answer:
Explanation:
Both df[df$age > 30, ] and subset(df, age > 30) are valid methods to subset rows based on a condition in R.