← Back to R Course | Chapter 3: Vectors | Lesson 5 of 7

Named Vectors

In this page:

  1. Named Vectors

Named Vectors

A vector's elements can have names, assigned with names() or directly with c(a = 1, b = 2), letting you access elements by name instead of position. Named vectors behave like a simple lookup table. Names are stored as an attribute alongside the vector's values.

Note: Named vectors are a lightweight alternative to a dictionary/hash map for simple key-value data.

Example: Named Vectors

markup
ages <- c(alice = 30, bob = 25, cara = 40)

print(ages)
cat("Bob's age:", ages["bob"], "\n")
print(names(ages))
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.