Boys and girls of the community (any community actually that is reading this). I am going to go over an often over looked expression in very simple terms, viz the for-loop. For those of you who do know it, it can be a great refresher and for those who don’t, well here’s your opportunity to learn something new. You just need to know some basic math, have a little imagination and we should be fine after. Hopefully at the end of this article you can appreciate the beauty of the title.
(Let me know if you got it?)
The compilation language I will be using in this article will be R but we will switch it up with Python and other IDEs from time to time. We will also meet a variety of tools of the trade for other types of problems.
Well where do we start? Well with math of course.
1 + 1
## [1] 2
1 + 2
## [1] 3
1 + 3
## [1] 4
1 + 4
## [1] 5
1 + 5
## [1] 6
You get the idea - from that simple expression above it’s pretty clear (hopefully) that a pattern emerged.
1 + (some number that seems to increase by 1) = (sum of those two numbers on the left-hand side)
Pretty straight-forward so far. Well let us say we needed to compute this 5 more times, instead of performing that mundane task, we can create an algorithm that does that for us and then ask our computers to compute that algorithm for us. But first, what would that algorithm look like? It will go something like this in pseudo-english:
\(1 + x = y\) where x goes from 1 to 10.
In mathematical notation:
\(1 + x = y\) where x:{1,..,10}
Well that does not quite give us the answers we are looking for does it? It just nicely summarizes it.
To compute it we use our trusty ‘FOR LOOP’, which does the tedious computation for us and produces the results.
Nicely put, it goes something like this:
for (variable in vector) {
operation
}
Using our example of \(1 + x = y\) where x:{1,..,10}
for (x in 1:10){
y = (1 + x)
print(y)
}
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
That print(y) function at the end there, just shows us each of the answers of 1 + x that was stored as the variable y.
At this point, you may be like, ‘This finna be a breeze!’. That may be true but for-loops can be used for various tasks ranging from simple to complex calculations and even out-of-the-box iterations.
Let us go through a couple examples:
t = c()
for (x in 1:100){
y = x^2
t = rbind(t,y)
}
print(head(t,10)) #Displaying first 10 results
## [,1]
## y 1
## y 4
## y 9
## y 16
## y 25
## y 36
## y 49
## y 64
## y 81
## y 100
print(tail(t,10)) #Displaying last 10 results.
## [,1]
## y 8281
## y 8464
## y 8649
## y 8836
## y 9025
## y 9216
## y 9409
## y 9604
## y 9801
## y 10000
You would have noticed in the above example, I introduced a column vector ‘t’. Well ‘t’ is just a nice index that allows us to store the last value of y and save it before y updates again as the loop continues.
Remember it is a loop after all and if not included it will just save the last value of y which occurs at x = 100.
for (x in 1:100){
y = x^2
}
print(y)
## [1] 10000
So sometimes, it is good to store things for easy reference. Let us say we wanted to reference x = 5 easily, then we can just ask the column vector t, to produce the fifth term.
t[5]
## [1] 25
And voila it gave when x = 5 which means y = \(5^2 = 25\)
Hope this all makes sense so far!
Let’s do a couple more examples that you can run through and see that we basically just created calculators of very popular functions. In fact that is the premise on which most calculators run for those functions we have come to love so much like squared terms, exponents, trigonometry functions.
Take a look at our beloved trig function, sine:
y1 = c()
for (x in 1:10){
y = sin(x)
y1 = rbind(y1,y)
}
print(y1[1])
## [1] 0.841471
Verify on your calculators (if you still own one) for sin(1) - or just simply google it. Notice the example just goes from 1 to 10 but we can go further that. Give it a go for yourself.
What if we wanted to loop multiple vectors? Is that possible?
Well of course it is! We can use for-loops to our hearts desire! Although depending on the size of the vectors there may be a time/storage factor for computation but we can afford to live and learn.
s1 = c()
for (y in 10:20){
for (x in 1:10){
s = x * y
s1 = rbind(s1,s)
}}
print(head(s1,20))
## [,1]
## s 10
## s 20
## s 30
## s 40
## s 50
## s 60
## s 70
## s 80
## s 90
## s 100
## s 11
## s 22
## s 33
## s 44
## s 55
## s 66
## s 77
## s 88
## s 99
## s 110
Can you figure out what this loop has done?
It took each value of y starting from 10 and multiplied it by x from 1 to 10, then went on to when y was 11 and multiplied it by each value of x again, all the way to y = 20. It fixed the outer loop for each of its values while moving through the inner values and once that is completed it repeats the cycle.
Interesting things!
I think we are in a much better place concerning for-loops for us to start doing even more interesting things with data, for we will be going from 0 to 100 (for loop reference) in the next feature article where I will be showing you how to use for-loops to perform a web-scrape on one of my favourite books and we will do some analyses on that book! Stay tuned!