Loops
Imagine that you want to calculate the mean of a vector. That is simple enough right? As we discussed in Functions, simply use the mean() function on that vector and you have the mean. However, what if you want to calculate the means of each column in a matrix? What if the matrix had 5,000 columns? You could manually calculate the mean 5,000 times, however, this would be a laborious task. Loops were created to do tasks multiple times in a very quick manner such that analyses could be improved dramatically. Three common loops are For loops, If loops, and If-Else loops.
For
Below is a video about For loops. If you would like to read instead, read on!
For loops do a task a certain amount of times. In other words, it repeats lines of code the number of times you ask it to. The way I remember what for loops do is by using for in sentence. Here is an example
For 100 people, Jen gave each person a cookie.
For a specified number, 100, Jen did a task, gave each person a cookie. Hopefully that makes the For loop a little bit more approachable.
The general format for a For loop in R is
for(i in 1:x){ #x represents the final ending number #what you want your for loop to do }
Let us practice using For loops on simple problems.
Example I
Create a For loop to increase each value in the following vector of data by 1. Make sure to comment each line of the code so that you know what each line is doing.
Vector |
1 |
2 |
3 |
4 |
5 |
Answer I
We created the final vector to be 2,3,4,5,6 by using the code provided below. We also provide the output in R below as well.
vec<-c(1,2,3,4,5) # defining vector where it is the first 5 numbers starting at 1 vec # displaying vector that we just defined for(i in 1:5){ vec[i]<-vec[i]+1 # increasing each value in the vector by 1 } vec # displaying the final vector
Example II
Have each value in the vector provided above be increased by the next value in the vector. Do not do this for the last value. Make sure to comment every line of code.
Answer II
We created the vector 3,5,7,5 by using the following code. We provided the output in the Figure below.
vec<-c(1,2,3,4,5) # defining vector where it is the first 5 numbers starting at 1 vec # displaying vector that we just defined ham<-c() #defining empty vector for(i in 1:4){ ham[i]<-vec[i+1] + vec[i] # combining 1 and 2 in spot 1 of the new vector, then 2 and 3 in spot 2 of the new vector, etc... } ham # displaying the final vector
Let us see an example of where this might be used in coding for R for a statistical problem.
Example III
Awesome Apps recently developed a new app to the market. The app is a related to a thriving Harry Potter fan community. To collect data on users, they put code into the app that will report the amount of time a user uses the app on a given day of the firm’s desire. Within the first year, the firm randomly collected the amount of time the app was used in minutes on 100 users on 100 different random days.
Calculate the mean amount of time spent on the app on each day. Provide a histogram, the mean and standard deviation of all the days, and a five number summary. The data is provided here where the columns are the days and the rows are the observations. What shape is the distribution? Provide a picture. By what theorem can we state this? What do we call the standard deviation of the mean?
Answer III
Using the data, we provide a histogram, mean, standard deviation and five number summary of the data. The code and output in R was provided below as well.
mydata<-read.table('loops_data1.csv', sep=',')#load data into R mydata<-as.matrix(mydata) means<-c()#establish vector to save means from mydata for(i in 1:100){ means[i]<-mean(mydata[i,]) #calculate the mean and save it into a new vector } hist(means, main = paste("Histogram of Means"), xlab = 'Means', col='navy', border='light blue') #displays histogram of means with appropriate Main and X-Axis title changes. Also I changed the colors to navy and light blue because I wanted to! Haha mean(means) #calculates the mean of the means sd(means) #calculates the standard deviation of the means fivenum(means) #calculates the five number summary of the means
Statistic | Value |
Mean | 101.508 |
Standard Deviation | 0.0781 |
Min | 101.294 |
Q1 | 101.4545 |
Median | 101.496 |
Q3 | 101.5628 |
Max | 101.7047 |
Notice in Figure 4 that the shape is approximately normal. Since each day came from a gamma distribution with 2 and 3 as parameters and transformed by adding 100 to each value, all the days come from the same distribution. Additionally, since we are calculating the mean as the desired statistic, the distribution will be normal by the Central Limit Theorem (CLT). Therefore, the standard deviation of the mean is called the standard error. If you were uncertain of this, click here to find out more about the CLT.
If
Below is a video about If Loops and If-Else loops. If you do not want to watch it, and instead would rather read, then go along you scallywag!
If loops do a task only if certain conditions are met. In other words, lines of code will only be executed if they satisfy some given condition. The way I remember what If loops do is by using for in sentence. Here is an example
If Jen has a cookie, she gives 1 away.
Jen will only do the task if she has a cookie.
For If loops, the general format is
if(put condition here){ #tell it what to do if condition is met }
You can have a variety of conditions. Many times, we have used conditions such as less than, greater than, equal to, etc.. Now let us see some simple examples on how to use if loops.
Example IV
We had an object defined to be the number 4. Create an If loop to increase the object by 1 until it is no longer less than 10. What will the final value be?
Answer IV
Using the code provided below, we have created an If loop statement to solve the problem. We also provided the output. The final value will be 5.
four<-c(4) #define an object to contain number four if(four<10){ #start if loop; the condition we want is if the object is less than 10 four<-four+1 #if the object is less than 10, then we add 1 to the object } four #call the four object to see what the final value is; we expect it to be 5
Example V
We are given the following vector. There is a mistake in the recording software where instead of putting 13, it makes them a 0. Fix the problem using an If loop. Comment all of your lines of code.
Answer V
We provide the solution to the problem by creating an If loop within a For loop. We provide the code and output below.
vec<-c(3,5,10, 22, 0) #define the given vector for(i in 1:length(vec)){ #start the for loop; this will let us go through each number in the vector #notice that instead of typing 5, I used the function length(), which counts how long the vector is for me if(vec[i]==0){ #start the if loop with the desired condition; notice that equals is == and NOT just = vec[i]<-13 #change the value to 13 if it equals 0 } } vec #displaying the final vector
If-Else
If-Else loops are exactly the same as If loops, except that they have an action to perform if the condition in the parentheses fails. Here is the general format of the If-Else loop:
if(put condition here){ #tell it what to do if condition is met } else{ #put the alternative action here if condition is not met }
Let us see some examples.
Example VI
Your computer program accidentally records all values off by 1. Specifically, it adds 1 to the negative values and subtract 1 from the positive value. Below is the data from the server. Fix the data in R. Comment on every line of code and use an If-Else loop.
Data |
-1 |
-1 |
-1 |
1 |
1 |
1 |
Answer VI
We provide the solution below alongside the output in R.
[Insert code 9 here]
vec<- c(-1,-1,-1, 1,1,1) #defining the vector of data for(i in 1:length(vec)){ #starting with a for loop to go through each number in the vector if(vec[i]<0){ #if the number is less than zero; defining condition vec[i]<-vec[i]-1 #subtract 1 from it } else{ #if it fails to meet the condition do this vec[i]<-vec[i]+1 #increase the number by 1 } } vec
Example VII
It was discovered that the program also replaces all the 13’s in the data with 0’s. Luckily, 0’s should not exist in the data. Therefore, correct this mistake using If-Else loops for the following data. Comment on all the lines of code.
Data |
-1 |
-1 |
-1 |
1 |
1 |
1 |
0 |
0 |
Answer VII
We provide the solution below alongside the output in R.
vec<- c(-1,-1,-1,1,1,1, 0, 0) #defining the vector of data for(i in 1:length(vec)){ #starting with a for loop to go through each number in the vector if(vec[i]<0){ #if the number is less than zero; defining condition vec[i]<-vec[i]-1 #subtract 1 from it } else{ #if it fails to meet the condition do this; notice that we have a loop within a loop- this is called a nested loop if(vec[i]>0){ #if loop where the condition is if the number is greater than 0 vec[i]<-vec[i]+1 #increase the number by 1 } else{ #if it fails the above condition, do this vec[i]<-13 #define the number, 0, to be 13 } } } vec