Using Pipes to Simplify Multi-Step Code
There is no doubt that we are in the midst of peak vacation season. When planning a trip, people use the web to find flights from on city to another. Unknowingly to vacation planners, this means that travel websites are competing for position #1 on search engine results pages (SERPs) to provide searchers with relevant information and to gain viable organic traffic to their sites.
Being an SEO myself, I am interested in seeing which websites are ranking in the #1 position on SERPs for such search queries or keywords. Thanks to Kaggle, I have bi-weekly SERP data for “flights” and “ticket” keywords from December 2018 through August 1st, 2019. I will focus on the most recent set of rankings, in order to see which travel websites are performing the best right now.
A Multiple Step Process
To actually find which travel websites performed the best will require multiple steps of code in R to figure out:
- Step 1: Once I’ve imported the dataset, I would need to filter the data to only include data observations that ranked in position #1.
- Step 2: From this, I would need to subset the data to only include the website links.
- Step 3: I would need to find the counts for the number of times each link appears in data. This would be equivalent to the number of times that the site appeared in position #1.
- Step 4: Once the counts are collected, I would have to arrange the counts in descending order.
- Step 5: Lastly, I would need to select just the top 10 largest counts to know which sites ranked the most for position #1.
If done separately, that would be quite a lot of code, right? Well, there is a way to consolidate all of this into one set of code; by using pipes.
What is a Pipe?
A pipe is an operator in R symbolized by %>% which allows you to chain together sequences of code. This will drastically simplify your code, because it will no longer need to be broken out into multiple steps to complete a single end result.
While the pipe is originally a part of the magrittr package, a package with the aim of decreasing development time and improving the readability of code, you will never need to actually install this package. This is because packages in the tidyverse will automatically load the pipe for you.
Let’s see how using pipes can turn all of steps from above into one set of code:
Observing each line of the code above, we can see that this captures each step of the process outlined above with our handy %>% operator separating each step. Let’s now see which travel websites ranked in position #1 the most often with a bar plot visualization:
From the bar plot, we can clearly see that we have a winner. Skyscanner appeared in position #1 on SERPs ~175 times on August 1st, followed by Kayak, which only appeared ~75 times for the specified keywords.
What does this mean for SEO? Skyscanner is clearly doing something right in order to perform so well, so analyzing what works with their site can benefit others in the SEO community. Not to mention, from a competitive standpoint, this kind of information would be highly beneficial to other travel websites looking to improve their performance on SERPs.
We just saw how implementing pipes into code can simplify the multi-step coding in R. Click here for reference code. Enjoy piping!