Using R for SEO, What to expect?
The power of R'. What's different about it?
R' is a high-level programming language that mainly focuses on data analysis. Meaning it's "specialized". With a few lines of code, you can do a lot. Let me give you an example:
These lines of code will :
prompt a select file menu for you to select a CSV (
file.choose
)It will import data inside R (
read.csv
) intointernal_linking
varThe second line will just display it (
View
)
Let's do it with a website links file
This is how you open and browse a file with 2.6 Million rows effortlessly. Noticed the small search icon on the top right? Yes, you can search within it quite easily too.
Want to count HTTP code? Here it is
You can recognize the View
function from before. the table
function just count values. the $
is a shortcut to access column values
It displays:
This is 30 secondes job. The most time-consuming part was finding the file on the hard disk.
Of course, this is just a silly example. There are countless ways to do this (third-party app, terminal, Excel pivot, panda/polar), but it gives a nice introduction to R's possibilities and how simple that is.
'There is a package for that'
The real power of R relies on R packages. What's a package you may ask? It's an on-demand library of functions you can load to help you in specialized tasks. Again let's take some examples.
⬢ ggplot2
ggplot2
It's one of the most famous packages. it can be used to build advanced charts and plots. To use it, you just have to install it once like this
to load it
and after that, you can now use it
Because we only want to see the problematic http codes, we are going to filter
Let's not go into details for now, but believe it or not, I'm not capable of writing this code, I just googled: "Bar charts chart ggplot" , "flip axis ggplot", ... shamelessly copy-paste the codes.
gggplot2 is powerful, it can make basically every chart you can think of
A few examples of plots done using ggplot2
To see more examples:
The R Graph Gallery
/
Top 50 ggplot2 Visualizations, some nice code to copy-pasteTidy Tuesday, nice to see how far ggplot2 can be pushed
Let's look at another package
⬢ Lubridate
Lubridate
Lubridate will help to deal with our timestamp values. After the now-classic installing and loading
It can be used to guess and transform this Time.stamp
into a real date format
Values have been transformed into a true Date
format.
No more "at" in the middle or "am/pm". It's now easier to read and sort. The dmy_hms
function guessed successfully that the "at" was useless.
Now that those are real dates and no longer character string, we can plot them using ggplot
the Lubridate
package can also help with duration, time zone, intervals, ... Have a look at the cheatsheets. It is a bit complex to get into but so much less than trying to do it yourself. I've lost literally days of my working life, trying to do this kind of stuff badly in Excel/Google Sheet.
⬢ urltools
urltools
One last example for the road. 'Want to extract links domains? You can sure use regex, or even try to split the string using "/" as a separator... OR you can use the more reliable urltools
package which as a dedicated domain()
function to do exactly that.
Let's check out the values, nearly the same code as before:
Where to find packages?
Good question! All the previous packages have been downloaded from CRAN. It's a repository that contains thousands of packages. Github is also a great source. There are so many that, the problem is often to find the right one. The way to go is usually to ask around using:
Twitter using the #rstats hashtag
There are a couple of nice slacks like the Measurecamp's one
The community is smaller than other programming languages but people are more willing to help, it compensates.
The confusing things about R
The name
Oh you do 'R programming', that's cool. Is it like Air Guitar? You do fake programming? - An anonymous member of my family
"R" is a weird name, especially in this covid time, and it's not the most Google-friendly name either. So here are few links to help find R resources.
https://rseek.org/ - R search engine
https://www.r-bloggers.com/ - R blogs aggregator
https://www.bigbookofr.com/ all the R free books
https://github.com/search?l=R&q=seo&type=code Search github for R source code
the <-
<-
If you've seen some R' code before and you might have been surprised to see this "<-" being used. it's just a legacy thing, historically R differentiates "assignation" and "comparison", example:
assignation - If you want to set the value of X to 3.
comparison - Is X equal to 3?
If you want to keep this little tradition alive you can use <- but it is really up to you. Perfectly fine to use =
The |>
or%>%
|>
or%>%
The (weird) |>
operator allows operations to be carried out successively. Meaning: the results of the previous command are the entries for the next one. Like the >
( “pipe”) command line for the terminal if you came across it. You might also see %>%
sometimes
Always better with an example, let's take the first line of code of this page
Its 3 functions are used one after the other. The readability is decent. I wouldn't recommend adding a fourth. the |> operator fixes this soon-to-be problem.
As you can see, fairly easy to read. This operator is so practical that most R practitioners now use it.
R' relies a lot on vectors which are confusing
Let's see some examples.
the good part is you don't need to make a loop every time you need to make some basic operations
Last updated