LOOP (programming language)

LOOP (programming language)

##########How to loop in R##########
#####################################
#1a. Introduction
#####################################
#Use the for loop if you want to do the same task a specific number of
#times.
#It looks like this.
#for (counter in vector) {commands}
#I’m going to set up a loop to square every element of my dataset, OddNum,
#which contains the odd integers
#from 1 to 100 (keep in mind that vectorizing would be faster for
#my trivial example – see below).
OddNum = seq(1, 100, by=2)
OddNum.squared = NULL
for (i in 1:50 ) {
OddNum.squared[i] = OddNum[i]^2
}
#############################
4. Function
############################
trial = function(n)
sum(runif(n) < 0.5) # could have done a binomial draw...
trial(23)
#If the creation of a new vector is the goal, first you have to set up
#a vector to store things in prior to running
#This is the foo.squared = NULL part.

#################################################################
#1b. Method 2
#################################################################
OddNum=seq(1,100, by=2)
OddNum .squared = numeric(length=50) #generates a vector of 50 zeros;
#now we run the loop as before
for(i in 1:50){
Sqr.OddNum[i]=OddNum[i]^2
}

##############################################################
# 2. Run faster compare to 2b
##############################################################
bar = seq(1,200000, by=2)
bar.squared = rep(NA, 200000)
for (i in 1:length(bar) ) {
bar.squared[i] = bar[i]^2
}
#get rid of excess NAs
bar.squared = bar.squared[!is.na(bar.squared)]
summary(bar.squared)

###########################################################
#2b. This same loop, but slow down the system
###########################################################
bar = seq(1, 200000, by=2)
bar.squared = NULL
for (i in 1:length(bar) ) {
bar.squared[i] = bar[i]^2
}
summary(bar.squared)

##################################################
3a. While Loop
#################################################
foo= NULL
i=1
while (i<5){
foo[i]=i
i = i+1
}

#################################################
# General Summary
#################################################
#a)##### For – Loop
#Assume you have estimated the following regression analysis:
#y = 10 + 5*X. By means of a forloop
#you predict the y’s corresponding to all possible X’s
#(ranging from 0 – 600). Save predicted y’s in vector y.
#b) #### While – Loop
#Assume you have estimated the following regression analysis:
#y = 10 + 5*X. By means of a whileloop
#you predict the y’s corresponding to all possible X’s as
#long as the predicted value is ≤ 3000. Save the predicted y’s.
######## c) Repeat – Loop
#Assume you have estimated the following regression analysis:
#y = 10 + 5*X. By means of a repeatloop
#you predict the y’s corresponding to all possible X’s as
#long as the predicted value is ≤ 3000. Save the predicted y’s.

Great days are ahead, hold to God

Like
Reply

To view or add a comment, sign in

More articles by Sayo Aluko

Others also viewed

Explore content categories