Get startedGet started for free

Vector names()

Let's return to the example about January and February's returns. As a refresher, in January you earned a 5% return, and in February, an extra 2% return. Being the savvy data scientist you are, you realize that you can put these returns into a vector! That would look something like this:

ret <- c(5, 2)

This is great! Now all of the returns are in one place. However, you could go one step further by adding names to each return in your vector. You do this using names(). Check this out:

names(ret) <- c("Jan", "Feb")

Printing ret now returns:

Jan Feb 
5   2

Pretty cool, right?

This exercise is part of the course

Introduction to R for Finance

View Course

Exercise instructions

  • Defined for you are a vector of 12 monthly returns, and a vector of month names.
  • Add months as names to ret to create a more descriptive vector.
  • Print out ret to see the newly named vector!

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Vectors of 12 months of returns, and month names
ret <- c(5, 2, 3, 7, 8, 3, 5, 9, 1, 4, 6, 3)
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

# Add names to ret
names(ret) <- 

# Print out ret to see the new names!
Edit and Run Code