# R for Excel Users: Pivot Tables, VLOOKUPs in R

Written by Matt Dancho

New business and financial analysts are finding R every day. Most of these new userRs (R users) are coming from a non-programming background. They have ample domain experience in functions like finance, marketing, and business, but their tool of choice is Excel (or more recently Tableau & PowerBI).

Learning R can be a major hurdle. You need to learn data structures, algorithms, data science, machine learning, web applications with Shiny and more to be able to accomplish a basic dashboard. This is a BIG ASK for non-coders. This is the problem I aim to begin solving with the release of tidyquant v1.0.0. Read the updated āR for Excel Usersā on Business Science.

Letās demo several of the new Excel features!

# Excel in R: Introducing tidyquant 1.0.0Pivot Tables, VLOOKUPs, Sum-Ifs, and more

In tidyquant version 1.0.0, I have added the following features to support new useRs transitioning from an Excel background.

## New Features To make the transition to R easier for Excel Users

Iāll showcase a small portion of the new features in this post. Register Here for new Learning Labs (Free) - I go through real business examples where I showcase packages like using Shiny and tidyquant together (did this in Learning Lab 30).

Shiny App with Pivot Table using tidyquant
Register for New Learning Labs Here

## Replication Requirements

Please use tidyquant (>= 1.0.0), which is now available on CRAN.

## ā Pivot Tables

The Pivot Table is one of Excelās most powerful features. Honestly, when I came to R, one of the biggest things I lost was the Pivot Table - A tool used for quickly summarizing data into a digestable table. Itās now in R with pivot_table().

### Excel Pivot Table

For those that may have never used the Excel Pivot Table before, the process goes something like this.

1. Start with a raw table in ālongā format where each row is a unique observation
2. Use the Excel Pivot Table dialog to position fields into Columns, Rows, and Summarized Values
3. The Pivot Table is returned with the data summarized into columns and rows

Excel Pivot Table is now in R

### R Pivot Table

Excelās Pivot Table now in R with pivot_table(). Letās try it out.

First, letās get some raw data. The FANG data set ships with tidyquant and represents the ouput of the tq_get(c("FB", "AMZN", "NFLX", "GOOG")) from 2013 to 2016. This is raw OHLCV data with adjusted stock prices downloaded from Yahoo Finance.

We can summarize this information with a Pivot Table using pivot_table(.rows, .columns, .values). First, Iāll take a look to see if there are any missing (NA) values. The only trick is to use a ~ in front of any calculations. All zeros is good.

AMZN FB GOOG NFLX
0 0 0 0

Next, Iāll do some financial summarizations. Iād like to take a look at percentage returns by year and quarter. This is easy to do by using stacked functions with the c() operator on .columns and .rows.

symbol QUARTER(date) 2013 2014 2015 2016
AMZN 1 0.0356768 -0.1547856 0.2060807 -0.0680544
AMZN 2 0.0614656 -0.0530919 0.1723923 0.1956892
AMZN 3 0.1082595 -0.0299348 0.1703285 0.1538281
AMZN 4 0.2425300 -0.0223965 0.2979913 -0.1038196
FB 1 -0.0864286 0.1010785 0.0480561 0.1162199
FB 2 -0.0254603 0.0745768 0.0502020 -0.0153369
FB 3 1.0245869 0.1613283 0.0344034 0.1233033
FB 4 0.0838954 0.0192031 0.1507422 -0.1065466
GOOG 1 0.0980851 0.0017401 0.0441873 0.0041923
GOOG 2 0.0988279 0.0143170 -0.0406450 -0.0770892
GOOG 3 -0.0134815 -0.0091132 0.1659128 0.1116688
GOOG 4 0.2634837 -0.0736798 0.2414403 -0.0009578
NFLX 1 1.0571677 -0.0297393 0.1941594 -0.0702983
NFLX 2 0.1571014 0.2081494 0.5901917 -0.1345316
NFLX 3 0.3786783 -0.0463327 0.1027844 0.0194477
NFLX 4 0.1341568 -0.2214904 0.0792602 0.2062750

A few points:

1. Collapsing - I just used functions to collapse the daily date by YEAR() and QUARTER(). This essentially creates a new grouping variable that is a part of the date.
2. Stacking - I stacked multiple grouping operations with the c() operator: .rows = c(symbol, ~ QUARTER(date))
3. Summarization Details - I added multiple function calls to get the Percentage Change in the .values summarization operation. This is allowed as long as the result returns a single value.
4. Tilde Required - For calculations (e.g. ~ YEAR(date)), I used the tilde (~) each time, which is required.
5. Tilde Not Required - For bare column names with no calculation, a tilde (~) is not required.

### My Favorite Part of Pivot Tables in R

We can easily switch Pivot Tables to provide different levels of summarization. Now Iāll quickly change to returns by year. Notice Iām using a new summarization function, PCT_CHANGE_FIRSTLAST() to save me some typing.

symbol 2013 2014 2015 2016
AMZN 0.5498426 -0.2201673 1.1907495 0.1772084
FB 0.9517858 0.4260647 0.3340983 0.1255136
GOOG 0.5495473 -0.0532416 0.4460024 0.0404130
NFLX 3.0014129 -0.0584587 1.2945491 0.1258640

## ā VLOOKUP

When I first started learning R, I couldnāt grasp how to merge / join data. It was very frustrating because I was used to Excelās VLOOKUP function that pulled a value or a column of values as needed.

### Excel VLOOKUP

VLOOKUP is an Excel function to lookup and retrieve data from a specific column in table. Hereās how the process works in Excel.

2. Simple Case - Use a VLOOKUP to input a single value and output a single value.
3. More Powerful Case - Use a VLOOKUP to add a column to an Excel Table.

Excel VLOOKUP() is now in R

### R VLOOKUP

The most popular Excel Reference Function, VLOOKUP, is now in R as VLOOKUP(). Itās vectorized, which means we can use VLOOKUP() inside of dplyr::mutate().

Letās replicate a VLOOKUP in R using the new VLOOKUP() function. First, letās create a simple lookup table.

stock company
AMZN Amazon
NFLX Netflix

### Simple VLOOKUP Case

First, letās mimic the āsimpleā case where we just want to lookup a Single Value.

So what happened? We supplied the string āAMZNā, and the VLOOKUP() function new to search the lookup_table matching the stock column and returning the company.

### More Powerful VLOOKUP Case

Letās try the more Powerful Case - pulling in a column of matched lookup values. We can do this by using the mutate() function from dplyr. This works because VLOOKUP() is vectorized.

### What Iām Most Excited About Using VLOOKUP for

I actually canāt wait to use VLOOKUP() in Shiny apps. There are many times when I want the user to input a variable (a Key), and internally on the Shiny Server convert it to something more useful in a table (a Value). I showcased this technique LIVE in Learning Lab 30 - Shiny + Tidyquant Apps. Register Here to attend new Learning Labs (FREE).

## ā Summarizing āIFSā Functions

One of the functions that many Excel Users (including myself) become accustomed to is filtering summations, which I refer to as āIFSā functions. These are very handy at quickly filtering on conditions while aggregating your data.

### Excel Sum-Ifās (and friends)

Excel has SUMIFS(), COUNTIFS(), AVERAGEIFS(), and several more Summarizing āIFSā Functions. Hereās how they work:

1. Develop a Condition to summarize: Sum Sales when Region = āEastā
2. Use one or more Conditioning Column(s) to develop a logical cases (e.g. region = āEastā)
3. Use a Summarizing Column to aggregate (e.g. SUMIFS(sales))
4. Put it together returning a single value: SUMIFS(sales, region, ā=Eastā)

Excel SUMIFS is now in R as SUM_IFS()

### R Sum-Ifās (and friends)

R now has a full suite of āIFSā functions. We can test them to get the basics.

#### SUM_IFS()

Summarizing things.

Counting things.

#### Used in tidyverse

Letās use COUNT_IFS() to count how many times high trade volume occurs in 2015. We can accomplish this shockingly easily by combining summarise() and the COUNT_IFS() function.

## ā 100+ New Excel Functions

100+ Excel-based statistical, date/date-time, and financial math functions have been ported to R. The things Iām most excited about are Business Calendar calculations:

• Business Holiday Calendars - Solves a major pain point with business date calculations. Integrations with lubridate and timeDate have enabled Holiday Date Sequences to automate calculation of Net Work Days and working periods.

• Financial Calculations - Iāve ported NPV, IRR, FV, PV, PMT, and RATE. Then I realized that thereās an amazing package called FinCal. The plan is to leverage FinCal going forward.

100+ Excel Functions now in R

Businesses rely on their ability to accurately predict revenue. A key driver is whether or not the business is open (shocker!). For a business thatās closed on weekends and standard business holidays, itās now super easy to calculate something simple like NET_WORKDAYS().

#### Net Working Days Example

When run with just a start and end, it returns the number of days excluding weekends.

But what about holidays? We have a new function called HOLIDAY_SEQUENCE() to calculate the business holidays between two dates (thanks to timeDate!).

Now we can simply remove these dates from the Net Workdays calculation. We get 126 days removing standard business holidays.

## ā NEW API Integration

The Tingo API is a popular free and open source for stock prices, cryptocurrencies, and intraday feeds from the IEX (Investors Exchange). Iām planning integration via the riingo package.

First, get a Tiingo API Key. Then setup your API Key using tiingo_api_key().

We can use tq_get() to interface with the Tiingo API. Here Iām getting High Frequency Time Series at 5-Minute Intervals via Tiingoās IEX data feed.

Learn more by visiting the help documentation for ?tq_get.

# Summary

Thereās a ton to learn. So much that I couldnāt possibly go over all of the new features in tidyquant v1.0.0 in this article. And, most importantly, you havenāt seen tidyquant tackle some real messy business problems.

I have good news. I taught all the Excel Features in Learning Lab 30 - Shiny + Tidyquant for Finance Apps (Register Here to attend new Learning Labs, Itās Free), I tackled some real financial data, showing how we can do really important things like:

• Perform Portfolio Analysis
• Use NEW Excel Features
• Build Shiny Apps with Pivot Tables, VLOOKUPs and SUMIFS.

# Register for Learning Labs (FREE)Learn amazing tools like Shiny & tidyquant

I give away a ton of free R training. Itās a program called Learing Labs. Sign up here. š