Question: Programming, StudioR Hi, I have downloaded from Yahoo finance some prices of stocks. Now I want to output them in a table. But with me
Programming, StudioR
Hi, I have downloaded from Yahoo finance some prices of stocks. Now I want to output them in a table. But with me the data are always under each other. Could you fix my code that each stock is separated in each column.
So it should look like this:
Year Facebook Amazon Apple Netflix Google 2020 1% 2% 3% 4% 5% 2019 2% 3% 4% 5% 9% . .
Here is my code:
library(tidyquant) library(timetk) library(ggplot2) library(tibble) library(select) library(dplyr) library(corrplot) library(tidyr) library(DT)
# Setting our stock symbols to a variable
tickers <- c("FB", "AMZN", "AAPL", "NFLX", "GOOG")
# Dowload the stock price data
multpl_stocks <- tq_get(tickers, from = "2013-01-01", to = "2018-03-01", get = "stock.prices")
multpl_stocks %>% ggplot(aes(x = date, y = adjusted, color = symbol)) + geom_line() + ggtitle("Price chart for multiple stocks")
multpl_stocks %>% ggplot(aes(x = date, y = adjusted)) + geom_line() + facet_wrap(~symbol, scales = "free_y") + # facet_wrap is used to make diff frames theme_classic() + # using a new theme labs(x = "Date", y = "Price") + ggtitle("Price chart FAANG stocks") #Calculating the yearly returns for multiple stocks
multpl_stock_yearly_returns <- multpl_stocks %>% group_by(symbol) %>% # We are grouping the stocks by symbol tq_transmute(select = adjusted, mutate_fun = periodReturn, period = 'yearly', col_rename = 'returns') datatable(multpl_stock_yearly_returns)
.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
