A named vector in R is a vector where each element has a name attached to it. These names help identify elements by name instead of just by position.
Click to reveal answer
beginner
How do you assign names to a vector in R?
You can assign names to a vector using the names() function. For example: names(x) <- c('a', 'b', 'c') assigns names 'a', 'b', and 'c' to the elements of vector x.
Click to reveal answer
beginner
How can you access elements of a named vector by name?
You can access elements by using the name inside square brackets. For example, if x is a named vector, x['name'] returns the element with the name 'name'.
Click to reveal answer
intermediate
What happens if you assign duplicate names to elements in a named vector?
If you assign duplicate names, R allows it but accessing by name returns all elements with that name. This can cause confusion, so unique names are recommended.
Click to reveal answer
beginner
How do you remove names from a named vector?
You can remove names by setting them to NULL using names(x) <- NULL. This makes the vector unnamed again.
Click to reveal answer
Which function is used to assign names to a vector in R?
Anames()
Bcolnames()
Crownames()
Ddimnames()
✗ Incorrect
The names() function is used to assign or get names of vector elements.
How do you access the element named 'age' in a named vector v?
Av[age]
Bv$age
Cv['age']
Dv.age
✗ Incorrect
You access named vector elements using square brackets and the name as a string: v['age'].
What will names(c(10, 20, 30)) return if no names are assigned?
Ac('', '', '')
Bc('10', '20', '30')
CAn error
DNULL
✗ Incorrect
If no names are assigned, names() returns NULL.
If a named vector has duplicate names, what happens when you access by that name?
AReturns only the first element with that name
BReturns all elements with that name
CReturns NULL
DThrows an error
✗ Incorrect
Accessing by a duplicate name returns all elements with that name.
How do you remove all names from a named vector x?
ABoth C and D
Bnames(x) <- ''
Cx <- unname(x)
Dnames(x) <- NULL
✗ Incorrect
You can remove names by setting names(x) <- NULL or by using unname(x).
Explain what a named vector is and how it can be useful in R programming.
Think about how names act like labels on elements.
You got /3 concepts.
Describe how to assign, access, and remove names from a vector in R.
Focus on the names() function and indexing.
You got /3 concepts.
Practice
(1/5)
1. What is a named vector in R?
easy
A. A vector that only contains numeric values
B. A vector where each element has a name label for easy access
C. A vector that cannot be modified after creation
D. A vector that stores only character strings
Solution
Step 1: Understand vector naming
Named vectors have names assigned to each element, making it easier to access elements by name.
Step 2: Compare options
Only A vector where each element has a name label for easy access correctly describes a named vector as having name labels for elements.
Final Answer:
A vector where each element has a name label for easy access -> Option B
Quick Check:
Named vector = vector with names [OK]
Hint: Named vectors have labels for elements, not just values [OK]
Common Mistakes:
Thinking named vectors only hold numbers
Confusing named vectors with immutable vectors
Assuming named vectors store only strings
2. Which of the following is the correct way to create a named vector in R?
easy
A. c(apple = 5, banana = 3, cherry = 7)
B. vector(apple:5, banana:3, cherry:7)
C. named_vector(5, 3, 7, names = c('apple', 'banana', 'cherry'))
D. c(5 = apple, 3 = banana, 7 = cherry)
Solution
Step 1: Recall named vector syntax
Named vectors are created using c(name = value) pairs.
Accessing a named vector element by name returns a named vector of length 1 with that element.
Step 2: Analyze the code output
fruits["banana"] returns a named vector with name 'banana' and value 5, shown as c(banana = 5).
Final Answer:
c(banana = 5) -> Option D
Quick Check:
Access by name returns named vector element [OK]
Hint: Accessing by name returns named vector element, not just value [OK]
Common Mistakes:
Expecting just the value without name
Confusing output with string name
Assuming it causes an error
4. Identify the error in the following R code that tries to create a named vector:
numbers <- c(1 = "one", 2 = "two", 3 = "three")
medium
A. Values and names are reversed; names should be strings on the left
B. The syntax uses numbers as names incorrectly; names must be on the left side
C. Numbers cannot be used as names in named vectors
D. The c() function cannot create named vectors
Solution
Step 1: Check named vector syntax
Named vectors use name = value pairs, with names on the left and values on the right.
Step 2: Analyze the code
The code uses numbers on the left and strings on the right, which reverses the expected order.
Final Answer:
Values and names are reversed; names should be strings on the left -> Option A
Quick Check:
Names = values, not values = names [OK]
Hint: Names must be on left side, values on right in c(name = value) [OK]
Common Mistakes:
Putting values on left and names on right
Thinking numbers cannot be names (they can as strings)
Believing c() can't create named vectors
5. Given the named vector temps <- c(Mon = 22, Tue = 25, Wed = 20, Thu = 23), how would you create a new named vector with only days where temperature is above 22?
hard
A. temps[">22"]
B. temps[temps < 22]
C. temps[temps > 22]
D. temps[temps == 22]
Solution
Step 1: Understand logical indexing with named vectors
You can use a logical condition inside square brackets to filter elements.
Step 2: Apply condition temps > 22
temps > 22 returns TRUE for days with temperature above 22, so temps[temps > 22] selects those days.
Final Answer:
temps[temps > 22] -> Option C
Quick Check:
Use logical condition inside brackets to filter named vectors [OK]
Hint: Use temps[temps > value] to filter named vectors by condition [OK]
Common Mistakes:
Using incorrect comparison operators
Trying to index by string '>22' which is invalid
Selecting equal or less than values instead of greater
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.