# tidyquant package

The `tidyquant`

package is focused on retrieving, manipulating, and scaling financial data analysis in the easiest way possible. To get the `tidyquant`

package and start working with it, you first have to install it.

```
install.packages("tidyquant")
```

This places it on your local computer. You then have to load it into your current R session. This gives you access to all of the functions in the package.

```
library(tidyquant)
```

These steps of installing and librarying packages are necessary for any CRAN package you want to use.

The exercise code is already written for you. You will explore some of the functions that `tidyquant`

has for financial analysis.

This is a part of the course

## “Intermediate R for Finance”

### Exercise instructions

The code is already written, but these instructions will walk you through the steps.

- First, library the package to access its functions.
- Use the
`tidyquant`

function,`tq_get()`

to get the stock price data for Apple. - Take a look at the data frame it returned.
- Plot the stock price over time.
- Calculate daily returns for the adjusted stock price using
`tq_mutate()`

. This function "mutates" your data frame by adding a new column onto it. Here, that new column is the daily returns. - Sort the returns.
- Plot the sorted returns. You can see that Apple had a few days of losses >10%, and a number of days with gains of >5%.

### Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

```
# Library tidquant
library(tidyquant)
# Pull Apple stock data
apple <- tq_get("AAPL", get = "stock.prices",
from = "2007-01-03", to = "2017-06-05")
# Take a look at what it returned
head(apple)
# Plot the stock price over time
plot(apple$date, apple$adjusted, type = "l")
# Calculate daily stock returns for the adjusted price
apple <- tq_mutate(data = apple,
select = "adjusted",
mutate_fun = dailyReturn)
# Sort the returns from least to greatest
sorted_returns <- sort(apple$daily.returns)
# Plot them
plot(sorted_returns)
```

This exercise is part of the course

## Intermediate R for Finance

Learn about how dates work in R, and explore the world of if statements, loops, and functions using financial examples.

If data structures like data frames and vectors are how you hold your data, functions are how you tell R what to do with your data. In this chapter, you will learn about using built-in functions, creating your own unique functions, and you will finish off with a brief introduction to packages.

Exercise 1: What are functions?Exercise 2: Function help and documentationExercise 3: Optional argumentsExercise 4: Functions in functionsExercise 5: Writing functionsExercise 6: Your first functionExercise 7: Multiple arguments (1)Exercise 8: Multiple arguments (2)Exercise 9: Function scope (1)Exercise 10: Function scope (2)Exercise 11: PackagesExercise 12: tidyquant package### What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.