In this post, we explore vectors in R through multiple-choice questions (MCQs). Vectors are one of the most fundamental data structures in R, used to store a sequence of elements of the same data type. Whether you’re working with numbers, characters, or logical values, understanding how to manipulate vectors is crucial for efficient data handling.
These questions will help you grasp key concepts like creating vectors, accessing elements, performing operations on vectors, and combining vectors. You’ll also learn about special functions in R, such as c()
, rep()
, and which()
, which are commonly used in vector manipulation.
If you’re new to R or looking to improve your knowledge of vectors, this quiz will guide you through the basics and provide valuable insights. Let’s get started and test your understanding of vectors in R!
1. What is a vector in R?
Answer:
Explanation:
In R, a vector is essentially a 1-dimensional array that can hold numeric, character, or logical data.
2. How do you create a vector in R?
Answer:
Explanation:
The c() function is used to create vectors in R by concatenating elements together.
3. Which function is used to combine two vectors in R?
Answer:
Explanation:
The c() function can also be used to combine two or more vectors into a single vector.
4. What is the result of combining a numeric and a character vector in R?
Answer:
Explanation:
When a numeric vector is combined with a character vector in R, the numeric elements are coerced into characters, resulting in a character vector.
5. How can you access the third element of a vector named 'vec' in R?
Answer:
Explanation:
Elements of a vector are accessed using square brackets and the index of the element, so 'vec[3]' accesses the third element.
6. How do you create a sequence from 1 to 10 in R?
Answer:
Explanation:
A sequence from 1 to 10 can be created either by using the colon operator '1:10' or the seq() function as 'seq(1, 10)'.
7. What is the purpose of the rep() function in R?
Answer:
Explanation:
The rep() function is used to replicate the elements of a vector a specified number of times.
8. How do you check if two vectors are equal in R?
Answer:
Explanation:
The '==' operator is used to compare two vectors element-wise in R.
9. Which function can you use to sort a vector in R?
Answer:
Explanation:
Both sort() and order() functions can be used to sort a vector in R, but they have different functionalities.
10. What is the result of the expression length(c(1, 2, 3, 4)) in R?
Answer:
Explanation:
The length() function returns the number of elements in a vector, so length(c(1, 2, 3, 4)) is 4.
11. How do you remove NA values from a vector in R?
Answer:
Explanation:
The na.omit() function removes NA values from a vector in R.
12. How do you calculate the sum of all elements in a numeric vector 'vec'?
Answer:
Explanation:
The sum() function calculates the sum of all elements in a numeric vector.
13. What does the which() function do with a logical vector in R?
Answer:
Explanation:
The which() function returns the indices of the vector where the elements are TRUE.
14. How do you create a vector of 10 consecutive numbers starting from 5 in R?
Answer:
Explanation:
seq(5, 15) creates a sequence of numbers from 5 to 15, which includes 10 consecutive numbers starting from 5.
15. Which vector type can store elements of different data types in R?
Answer:
Explanation:
In R, a list can store elements of different data types, including numbers, characters, and even other lists or data frames.