R Vectors MCQ

1. What is a vector in R?

a) A list of numbers
b) A 1-dimensional array
c) A type of function
d) A data frame column

Answer:

b) A 1-dimensional array

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?

a) vector()
b) c()
c) list()
d) array()

Answer:

b) c()

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?

a) append()
b) concat()
c) merge()
d) c()

Answer:

d) c()

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?

a) Numeric vector
b) Character vector
c) Error
d) List

Answer:

b) Character vector

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?

a) vec[3]
b) vec(3)
c) vec{3}
d) third(vec)

Answer:

a) vec[3]

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?

a) 1:10
b) seq(1, 10)
c) c(1, 10)
d) Both a and b

Answer:

d) Both a and b

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?

a) To replace elements in a vector
b) To replicate elements in a vector
c) To report errors in a vector
d) To reverse the order of elements in a vector

Answer:

b) To replicate elements in a vector

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?

a) equal()
b) ==
c) is.equal()
d) compare()

Answer:

b) ==

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?

a) sort()
b) order()
c) arrange()
d) Both a and b

Answer:

d) Both a and b

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?

a) 1
b) 4
c) 3
d) Error

Answer:

b) 4

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?

a) na.omit()
b) omit.na()
c) remove.na()
d) !is.na()

Answer:

a) na.omit()

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'?

a) sum(vec)
b) total(vec)
c) accumulate(vec)
d) add(vec)

Answer:

a) sum(vec)

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?

a) Finds the length of the vector
b) Returns the indices of TRUE values
c) Converts logical values to numeric
d) Reverses the logical values

Answer:

b) Returns the indices of TRUE values

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?

a) 5:15
b) seq(5, 15)
c) seq(5, 14)
d) c(5, 10)

Answer:

b) seq(5, 15)

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?

a) Numeric vector
b) Character vector
c) Atomic vector
d) List

Answer:

d) List

Explanation:

In R, a list can store elements of different data types, including numbers, characters, and even other lists or data frames.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top