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.
The three tyes are basic data are numeric, character, and logical. The data type that would contain 3.1 is numeric.
What are R’s five most common types of objects? Type your answer below.
The 5 most common objects in R are vectors, matrices, data frames, factors, and lists.
Compare vectors and matrices. What are similarities and differences? Type your answer below.
A vector is a collection of data and a matrix in a collection of vectors.
Vectors and matrices are the same in the sense they can store data and display them. Vectors and matrices are different because vectors can show one set of data and matrix can show a set of different data.
Compare matrices and data frames. What are similarities and differences? Type your answer below.
A matrix is a pool of data and a data frame is table of data with rows and columbs.
Matrices and data frames are similar because they can store data and display them in an organized manner. Matrices and data frames are different because a matric can only display the data stored within the vector provided for it. When a data frame can display many different types of data in a customized manner displayed through rows and columbs.
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 12 months of returns, and month names
returns <- c(1, 2, 3, 4, 5)
days <- c("monday", "tuesday", "wednesday", "thursday", "friday")
# Create a data frame, monthly_returns
daily_returns <- data.frame(days, returns)
# Print out monthly_returns
daily_returns
## days returns
## 1 monday 1
## 2 tuesday 2
## 3 wednesday 3
## 4 thursday 4
## 5 friday 5
Subset rows with daily returns greater than or equal to 4%.
# Subset rows with ret greater than or equal to 7%
subset(daily_returns, returns >= 4)
## days returns
## 4 thursday 4
## 5 friday 5