Using R for SEO, What to expect?
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:
internal_linking = read.csv(file.choose())
These lines of code will :
- prompt a select file menu for you to select a CSV (
- It will import data inside R (
- The second line will just display it (
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.
search for dead links using http code
Want to count HTTP code? Here it is
You can recognize the
Viewfunction from before. the
tablefunction just count values. the
$is a shortcut to access column values
count of http code
This is 30 secondes job. The most time-consuming part was finding the file on the hard disk.
Of course, these are just some silly examples. There is countless way to do this thing (third party app, terminal, excel pivot, panda but it gives a nice intro of R' possibilities and how simple that is.
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.
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
aes(x = Status, fill = Status) +
Because we only want to see the problematic http codes, we are going to filter
internal_linking_filtered <- filter(internal_linking, !(Status %in% c("200 no error", "Not checked","999 LinkedIn blocking automated testing")))
aes(x = Status, fill = Status) +
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
To see more examples:
Let's look at another package
It can be used to guess and transform this
Time.stampinto a real date format
internal_linking$real_date = dmy_hms(internal_linking$Time.stamp)
Values have been transformed into a true
before and after using Lubridate function
No more "at" in the middle or "am/pm". It's now easier to read and sort. The
dmy_hmsfunction guessed successfully that the "at" was useless.
Now that those are real dates and no longer character string, we can plot them using
aes(x = real_date) +
the number of links discovered per date.
Lubridatepackage 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.
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
urltoolspackage which as a dedicated
domain()function to do exactly that.
# Installing and Loading Package
# extract domain and feed it to a new data column called 'domain'
internal_linking$domain <- domain(internal_linking$URL)
Let's check out the values, nearly the same code as before:
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
The community is smaller than other programming languages but people are more willing to help, it compensates.
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.
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.
x <- 3
comparison - Is X equal to 3?
x == 3
If you want to keep this little tradition alive you can use <- but it is really up to you. Perfectly fine to use =
x = 3
# same as
x <- 3
|>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
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.
# equivalent to the previous instruction
file.choose() |> read.csv() |> View()
# again equivalent
As you can see, fairly easy to read. This operator is so practical that most R practitioners now use it.
Let's see some examples.
#this instruction combine 3 numbers to make a vector and define the x variable.
x <- c(1,2,3)
# this will display our vector
# this will concatenate the x vector twice
# Unlike tables, vectors first element need to be called with 1
the good part is you don't need to make a loop every time you need to make some basic operations
#this will add one to all vector elements
y <- x+1
#Want to add up two vectors with each other? this will work
# it also works with function
x <- as.double(x)