Disclaimer: The content of this RMarkdown note came from a course called Introduction to R for Finance in datacamp.
What are R’s three most basic data types? Of the three, what would be decimal numbers like 3.1? Type your answer below.
numeric, character, logical numeric would be decimal numbers
What are R’s five most common types of objects? Type your answer below.
vector, matrix, data frame, factor, list
Compare vectors and matrices. What are similarities and differences? Type your answer below.
A vector is a collection of data that are all the same type, a matrix is similar but has rows and columns and only one data type
Compare matrices and data frames. What are similarities and differences? Type your answer below.
They are the same but a data frame can hold more than one data type.
Create a vector, “returns”, in place of “ret”; and assign 5 return values, 1 through 5 in place of (5, 2, 3, 7, 8, 3, 5, 9, 1, 4, 6, 3). Create another vector, “days”, in place of “months”; and assign 5 days, Monday through Friday in place of (“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”). Then, create a data frame, “daily_returns”, in place of “monthly_returns” by combining days and returns.
# Vectors of 5 days of returns, and day names
returns <- c(1, 2, 3, 4, 5)
days <- c("mon", "tues", "wed", "thurs", "fri")
# Create a data frame, daily_returns
daily_returns <- data.frame(days, returns)
# Print out daily_returns
daily_returns
## days returns
## 1 mon 1
## 2 tues 2
## 3 wed 3
## 4 thurs 4
## 5 fri 5
Subset rows with daily returns greater than or equal to 4%.
# Subset rows with return greater than or equal to 4%
subset(daily_returns, returns >= 4)
## days returns
## 4 thurs 4
## 5 fri 5
# Set the global option
knitr::opts_chunk$set(message = FALSE, warning = FALSE, collapse = TRUE, echo = FALSE, results = 'markup')