Menu
Coddy logo textTech

Named Vectors

Part of the Fundamentals section of Coddy's R journey — lesson 62 of 78.

So far, you've accessed vector elements using numeric positions. But what if you could access them by meaningful names instead? Named vectors let you assign labels to each element, making your code more readable and self-documenting.

You can create a named vector by providing names during creation:

ages <- c(alice = 25, bob = 30, charlie = 28)
print(ages)

Output:

  alice     bob charlie 
     25      30      28

To access elements by name, use the name as a string inside square brackets:

print(ages["bob"])

Output:

bob 
 30

You can also add names to an existing vector using the names() function:

scores <- c(85, 92, 78)
names(scores) <- c("math", "science", "english")
print(scores["math"])

Output:

math 
  85

Named vectors are especially useful when working with data that has natural labels, like storing student grades by subject or prices by product name.

challenge icon

Challenge

Easy

You will receive two lines of input:

  1. A comma-separated list of product names (e.g., laptop,phone,tablet,headphones)
  2. A comma-separated list of corresponding prices (e.g., 999,699,449,199)

Perform the following operations:

  1. Create a numeric vector from the prices
  2. Assign the product names to the vector using the names() function
  3. Print the entire named vector
  4. Print the price of the second product by accessing it using its name
  5. Print the price of the last product by accessing it using its name

For example, if the inputs are:

apple,banana,orange,grape
2,1,3,4

The output should be:

apple banana orange  grape 
    2      1      3      4 
banana 
     1 
grape 
    4

Note: Access the second and last elements by their names (as strings), not by numeric index.

Cheat sheet

Named vectors allow you to assign labels to each element, making elements accessible by meaningful names instead of just numeric positions.

Create a named vector by providing names during creation:

ages <- c(alice = 25, bob = 30, charlie = 28)

Access elements by name using the name as a string inside square brackets:

ages["bob"]  # Returns the element named "bob"

Add names to an existing vector using the names() function:

scores <- c(85, 92, 78)
names(scores) <- c("math", "science", "english")
scores["math"]  # Returns 85

Try it yourself

# Read input
con <- file("stdin", "r")
products_input <- suppressWarnings(readLines(con, n = 1))
prices_input <- suppressWarnings(readLines(con, n = 1))
close(con)

# Parse the comma-separated inputs
product_names <- strsplit(products_input, ",")[[1]]
price_values <- strsplit(prices_input, ",")[[1]]

# TODO: Write your code below
# 1. Create a numeric vector from the prices
# 2. Assign the product names to the vector using names()
# 3. Print the entire named vector
# 4. Print the price of the second product by accessing it using its name
# 5. Print the price of the last product by accessing it using its name
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals