Recall & Review
beginner
What is a named vector in R?
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?
✗ 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?✗ 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?✗ Incorrect
If no names are assigned,
names() returns NULL.If a named vector has duplicate names, what happens when you access by that name?
✗ Incorrect
Accessing by a duplicate name returns all elements with that name.
How do you remove all names from a named vector
x?✗ 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.