I am trying to knit the following code in my RStudio notebook and I keep getting this error:
Here is the code in question:
```{r}
my_stocks <- read.csv("stocks.csv",header=TRUE, stringsAsFactors = FALSE)
```
```{r warning=FALSE, message=FALSE, echo=FALSE}
library(tidyr)
library(dplyr)
library(ggplot2)
library(tidyverse)
library(lubridate)
```
How many different stocks are there (by stock symbol) (5)?
```{r}
my_stocks %>% count(stock_symbol, name = "Amount_of_stock", sort = TRUE) %>%
print(n-Inf)
```
#Out of all the stocks, there are 25 different types of stocks in the stocks dataset.
What are those stocks (5)?
```{r}
unique(my_stocks$stock_symbol)
```
#The following stocks are: XL, XFJ, XCO, XVG, XFB, XOM, XKN, XVF, XTO, XJT, XAA, XFP, XKO, XKK, XEC, XRX, XFL, XIN, XFH, XKE, XCJ, XRM, XEL, XFD, and XFR
Which stock had the highest single gain in a day, which day was it (10)?
```{r}
my_stocks %>%
mutate(daily_returns = stock_price_close / stock_price_open - 1) %>%
select(daily_returns,stock_symbol, date) %>%
arrange(desc(daily_returns)) %>%
print(n=10)
```
#The stock with the highest single gain in a day was XKN with 93% gain on 10/10/2008
Which stock had the highest single loss in a day, which day was it (10)?
```{r}
my_stocks %>%
mutate(daily_returns = stock_price_close / stock_price_open - 1) %>%
select(daily_returns,stock_symbol, date) %>%
arrange(daily_returns, decreasing = FALSE) %>%
print(n=10)
```
#The highest single loss in a day was XRM with a 72% loss on 3/18/2008
Create a plot of closing price for XOM over time (5)
```{r}
my_stocks$year <- year(mdy(my_stocks$date))
xom_plot <- my_stocks %>% filter((stock_symbol == "XOM") & (year >= 2009 & year <=2010)) %>%
ggplot(aes(x=date, y=stock_price_close, group=1)) +
geom_line() + theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())
xom_plot <- xom_plot + ggtitle("XOM Closing Prices Year 2009 thru 2010")+ theme(plot.title = element_text(hjust = 0.5)) + xlab("Trading Day") + ylab("Closing Price") +scale_y_continuous(labels=scales::dollar_format())
xom_plot
```
#The chart above shows the closing price for the stock XOM from 2009 through 2010
Create a plot of closing price for XKK over time (5)
```{r}
my_stocks$year <- year(mdy(my_stocks$date))
xkk_plot <- my_stocks %>% filter((stock_symbol == "XKK") & (year >= 2009 & year <=2010)) %>%
ggplot(aes(x=date, y=stock_price_close, group=1)) +
geom_line() + theme(axis.text.x=element_blank(),axis.ticks.x=element_blank())
xkk_plot <- xkk_plot + ggtitle("XKK Closing Prices Year 2009 thru 2010")+ theme(plot.title = element_text(hjust = 0.5)) + xlab("Trading Day") + ylab("Closing Price") +scale_y_continuous(labels=scales::dollar_format())
xkk_plot
```
#The chart above shows the closing price for the stock XOM from 2009 through 2010
Create a plot of the change in price per day for both XOM and XKK (10)
```{r}
my_stocks$year <- year(mdy(my_stocks$date))
xom_plot <- my_stocks %>% filter((stock_symbol == "XOM") & (year >= 2009 & year <=2010)) +
xkk_plot <- my_stocks %>% filter((stock_symbol == "XKK") & (year >= 2009 & year <=2010)) %>%
ggplot(xom_plot, aes(date,stock_price_close)) +
geom_line(aes(color="xom_plot")) +
geom_line(data=xkk_plot,aes(color="xkk_plot")) +
labs(color="Legend") +
scale_colour_manual("", breaks = c("XOM", "XKK"),
values = c("blue", "brown")) +
ggtitle("Closing Stock Prices: XOM and XKK")
theme(plot.title = element_text(lineheight=.7, face="bold"))
```
Could not figure this one out, but here is my attempt at it