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?
Este exercício faz parte do curso
Introduction to R for Finance
Instruções do exercício
- Defined for you are a vector of 12 monthly returns, and a vector of month names.
- Add
months
as names toret
to create a more descriptive vector. - Print out
ret
to see the newly named vector!
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# 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!