LoslegenKostenlos loslegen

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?

Diese Übung ist Teil des Kurses

Introduction to R for Finance

Kurs anzeigen

Anleitung zur Übung

  • 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!

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# 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!
Code bearbeiten und ausführen