c()ombine
Now is where things get fun! It is time to create your first vector. Since this is a finance oriented course, it is only appropriate that your first vector be a numeric vector of stock prices. Remember, you create a vector using the combine function, c()
, and each element you add is separated by a comma.
For example, this is a vector of Apple's stock prices from December, 2016:
apple_stock <- c(109.49, 109.90, 109.11, 109.95, 111.03, 112.12)
And this is a character vector of bond credit ratings:
credit_rating <- c("AAA", "AA", "BBB", "BB", "B")
This is a part of the course
“Introduction to R for Finance”
Exercise instructions
- Another example of a numeric vector for IBM stock prices is shown for you.
- Create a character vector of the
finance
related words "stocks", "bonds", and "investments", in that order. - Create a logical vector of
TRUE
,FALSE
,TRUE
in that order.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Another numeric vector
ibm_stock <- c(159.82, 160.02, 159.84)
# Another character vector
finance <-
# A logical vector
logic <-
This exercise is part of the course
Introduction to R for Finance
Learn essential data structures such as lists and data frames and apply that knowledge directly to financial examples.
In this chapter, you will learn all about vectors and matrices using historical stock prices for companies like Apple and IBM. You will then be able to feel confident creating, naming, manipulating, and selecting from vectors and matrices.
Exercise 1: What is a vector?Exercise 2: c()ombineExercise 3: Coerce itExercise 4: Vector names()Exercise 5: Visualize your vectorExercise 6: Vector manipulationExercise 7: Weighted average (1)Exercise 8: Weighted average (2)Exercise 9: Weighted average (3)Exercise 10: Vector subsettingExercise 11: Matrix - a 2D vectorExercise 12: Create a matrix!Exercise 13: Matrix <- bind vectorsExercise 14: Visualize your matrixExercise 15: cor()relationExercise 16: Matrix subsettingWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.