Test your knowledge of the R programming language, Part 2
Posted on
November 21, 2023
by
‍
How much do you know about the R programming language?

Last month, we featured a number of questions focused on troubleshooting the R language and it is time to step back and look at the basics. As R has grown in popularity and acceptance in data analysis circles, it has become a powerful and flexible tool and being able to use it correctly can be the difference between unearthing insights from data or finding nothing.

How much do you know about this language and its capabilities? What follows are a series of questions of varying levels of difficulty on the topic as it relates to coding basics. The answers appear at the end of the questions. Good luck!

1. In R, what is the primary difference between a vector and a list?
A) Vectors are used for numeric data, while lists are used for character data.
B) Vectors can only contain integers, while lists can contain any data type.
C) Vectors are one-dimensional, while lists can have multiple dimensions.
D) Vectors can have names for their elements, while lists cannot.

2. What function can be used to reorder the levels of a factor in R based on a specific criterion, such as frequency or custom order?
A) sort_levels()
B) reorder_factor()
C) levels_order()
D) factor_sort()

3. What function is used in R to concatenate multiple strings into a single string?
A) concat()
B) merge()
C) paste()
D) combine()

4. How do you access a specific element within an R vector?
A) Using the element() function
B) Using the [index] notation
C) Using the access() function
D) Using the $() notation

5. What function in R can you use to customize the color of points in a scatter plot?
A) col()
B) colorize()
C) point_color()
D) color_points()

6. What does the "levels" of a factor represent in R?
A) The number of unique elements in the factor
B) The order in which the factor levels were created
C) The data type of the factor
D) The distinct categories or labels of the factor

7. In R, what is a key characteristic of matrices that distinguishes them from data frames?
A) Matrices can have named columns, while data frames cannot.
B) Matrices must contain only elements of the same data type.
C) Matrices can have row names, while data frames cannot.
D) Matrices can store mixed data types, while data frames cannot.

8. The following code is intended to allow the user to input their name and then echo back β€œHello” and their name but it does not work properly. What is a likely cause of this error?
# Prompt the user to enter a name
name <- lineread("Enter your name: ")
# Print a greeting using the entered name
cat("Hello", name, "\n")

A) lineread should be readline
B) name should be $name
C) cat should be lineprint
D) There is no error – the code should work properly

9. You want to remove leading and trailing whitespace from a string in R. Which function should you use?
A) str_trim()
B) str_remove()
C) trim_whitespace()
D) remove_spaces()

10. In R, what function is used to add a title to a plot?
A) main()
B) title()
C) add_title()
D) plot_title()

11. You want to execute a block of code in R based on multiple conditions. Which construct can you use to handle this situation?
A) for loop
B) if-else statement
C) while loop
D) repeat loop

12. You are using the following line of code for the user to enter a number at a prompt:
num <- as.numeric(readline("Enter a number: "))
You next want to make sure what they entered is a positive integer. Which of the following test options will best accomplish this?
A) is.infinite(num)
B) grepl(num, "^[0-9]+$")
C) (is.numeric(num) && num > 0 && floor(num) == num)
D) class(num) == "numberic"

13. Which function is used to create a matrix from given data in R?
A) list()
B) data.frame()
C) matrix()
D) array()

14. When the summary() function is used on a dataset, it will provide a summary of the data, including:
A) kurtosis
B) first and third quartile
C) skew
D) standard deviation

15. Which of the following is true about R vectors?
A) Vectors can contain elements of different data types.
B) Vectors can only store numeric values.
C) Vectors cannot be indexed.
D) Vectors must contain only elements of the same data type.

Please visit GoCertify to attempt the remaining 10 questions of this quiz.


‍

ANSWERS

Last month, we featured a number of questions focused on troubleshooting the R language and it is time to step back and look at the basics. As R has grown in popularity and acceptance in data analysis

1. C: In R, the primary difference between a vector and a list is that vectors are one-dimensional, while lists can have multiple dimensions.
‍2. B: The reorder_factor() function can be used to reorder the levels of a factor in R based on a specific criterion, such as frequency or custom order.
‍3. C: The paste() function is used in R to concatenate multiple strings into a single string.
‍4. B: Using the [index] notation, you can access a specific element within an R vector.
‍5. A: The col() function can be used to customize the color of points in a scatter plot.
‍6. D: The "levels" of a factor in R represents the distinct categories or labels of the factor.
‍7. B: In R, matrices must contain only elements of the same data type.
‍8. A: The lineread function does not exist and should be readline.
‍9. A: The str_trim() function can be used to remove leading and trailing whitespace from a string in R.
‍10. A: The main() function can be used to add a title to a plot.
‍11. B: The if-else statement can be used to execute a block of code in R based on multiple conditions.
‍12. C: The (is.numeric(num) && num > 0 && floor(num) == num) check will see if the entered value is a number and greater than zero and return TRUE if all conditions are met. While all of the other options could also be used to see if the variable is a number, the syntax is incorrect in each case.
‍13. C: The matrix() function is used to create a matrix from given data in R.
‍14. B: The summary() function provides a summary of the data, including the first and third quartile as well as the minimum, median (which is the second quartile), mean, and maximum.
‍15. D: In R, vectors must contain only elements of the same data type.

‍

About the Author

Emmett Dulaney is a professor at Anderson University and the author of several books including Linux All-in-One For Dummies and the CompTIA Network+ N10-008 Exam Cram, Seventh Edition.

Posted to topic:
Certification

Important Update: We have updated our Privacy Policy to comply with the California Consumer Privacy Act (CCPA)

CompTIA IT Project Management - Project+ - Advance Your IT Career by adding IT Project Manager to your resume - Learn More