A vector in R is a basic data structure that holds elements of the same type in a sequence. It is like a row of boxes where each box holds one item, and all items are of the same kind.
Click to reveal answer
beginner
Why are vectors considered fundamental in R?
Vectors are fundamental because all other data structures in R, like matrices, lists, and data frames, are built on top of vectors. They provide a simple and efficient way to store and manipulate data.
Click to reveal answer
intermediate
How does R handle different types of data in vectors?
R vectors can only hold one type of data at a time (numeric, character, logical, etc.). If you mix types, R will convert them to a common type to keep the vector consistent.
Click to reveal answer
beginner
What happens if you combine numbers and characters in a vector?
R converts all elements to characters because character is a more general type. This process is called type coercion.
Click to reveal answer
intermediate
How do vectors improve performance in R?
Vectors allow R to store data in a contiguous block of memory, making data access and operations faster and more efficient compared to other data structures.
Click to reveal answer
What type of elements can a vector in R hold?
AOnly numeric elements
BElements of different types mixed freely
COnly elements of the same type
DOnly character elements
✗ Incorrect
Vectors in R hold elements of the same type. If different types are combined, R coerces them to a common type.
Which data structure is NOT built on vectors in R?
AFunctions
BMatrices
CLists
DData frames
✗ Incorrect
Functions are not built on vectors. Data frames, matrices, and lists are built using vectors.
What happens when you combine numeric and character data in a vector?
AAll elements become characters
BAll elements become numeric
CAn error occurs
DThe vector holds mixed types without change
✗ Incorrect
R coerces all elements to characters to keep the vector consistent.
Why are vectors efficient in R?
ABecause they are only used for small data
BBecause they allow mixed data types
CBecause they are slow to access
DBecause they store data in continuous memory blocks
✗ Incorrect
Continuous memory storage allows faster data access and operations.
Which of these is a characteristic of vectors in R?
AThey can only hold logical values
BThey are the building blocks for other data structures
CThey cannot be indexed
DThey store data randomly in memory
✗ Incorrect
Vectors are the foundation for other data structures like matrices and data frames.
Explain why vectors are the fundamental data structure in R.
Think about how R organizes data and builds complex structures.
You got /4 concepts.
Describe what happens when you mix different data types in a vector.
Consider how R keeps vectors uniform.
You got /4 concepts.
Practice
(1/5)
1. Why are vectors considered the fundamental data structure in R?
easy
A. Because they are only used for storing single values
B. Because they can hold different types of data in one vector
C. Because they hold many values of the same type in order
D. Because they are slower to process than lists
Solution
Step 1: Understand vector properties in R
Vectors store multiple values but all must be of the same type, arranged in order.
Step 2: Compare with other data structures
Unlike lists, vectors cannot hold mixed types, and they are the basic building block for data in R.
Final Answer:
Because they hold many values of the same type in order -> Option C
Quick Check:
Vectors = same type, ordered values [OK]
Hint: Remember vectors hold same-type ordered values [OK]
Common Mistakes:
Thinking vectors can hold mixed data types
Confusing vectors with lists
Assuming vectors store single values only
2. Which of the following is the correct way to create a vector of numbers 1, 2, and 3 in R?
easy
A. vector(1, 2, 3)
B. array(1, 2, 3)
C. list(1, 2, 3)
D. c(1, 2, 3)
Solution
Step 1: Recall vector creation syntax in R
The function c() combines values into a vector.
Step 2: Check other options
vector() creates an empty vector with specified length and mode, list() creates a list, and array() creates multi-dimensional arrays.
Final Answer:
c(1, 2, 3) -> Option D
Quick Check:
Use c() to create vectors [OK]
Hint: Use c() to combine values into a vector [OK]
Common Mistakes:
Using vector() with values directly
Confusing list() with vector creation
Using array() for simple vectors
3. What will be the output of the following R code?
v <- c(10, 20, 30)
typeof(v)
medium
A. "double"
B. "integer"
C. "list"
D. "character"
Solution
Step 1: Understand default numeric type in R vectors
By default, numbers with decimals or without are stored as double precision floating point numbers.
Step 2: Check the vector values
The vector contains 10, 20, 30 which are numeric literals without explicit integer suffix, so they are doubles.
Final Answer:
"double" -> Option A
Quick Check:
Numeric vectors default to double type [OK]
Hint: Numeric vectors default to double unless specified [OK]
Common Mistakes:
Assuming numeric literals are integers by default
Confusing list type with vector type
Expecting character type from numbers
4. Identify the error in the following R code that tries to create a vector:
v <- c(1, "two", 3)
medium
A. The vector contains mixed types, so R converts all to character
B. The code will cause a syntax error
C. The vector will only contain numeric values
D. The vector will be empty
Solution
Step 1: Analyze vector type coercion rules
R forces all elements in a vector to be the same type, so it converts numbers to characters if any element is character.
Step 2: Check the given vector elements
Since "two" is a character, 1 and 3 are converted to characters, so the vector becomes character type.
Final Answer:
The vector contains mixed types, so R converts all to character -> Option A
Quick Check:
Mixed types in vector cause coercion to character [OK]
Hint: Mixed types in vector become character type [OK]
Common Mistakes:
Expecting syntax error from mixed types
Assuming numeric values stay numeric
Thinking vector will be empty
5. You want to create a vector that holds the ages of a group of people: 25, 30, 22, and 28. Then you want to add 5 years to each age. Which R code correctly does this using vectors?
hard
A. ages <- c(25, 30, 22, 28)
ages <- ages - 5
B. ages <- c(25, 30, 22, 28)
ages <- ages + 5
C. ages <- c(25, 30, 22, 28)
ages <- ages * 5
D. ages <- list(25, 30, 22, 28)
ages <- ages + 5
Solution
Step 1: Create a numeric vector of ages
Use c() to create a vector holding the ages.
Step 2: Add 5 to each element
Adding a number to a vector adds it to each element element-wise.