Loops in R

Copy and paste the following code to your R Studio platform or R version:

# Loops in R
# Some more examples using loops
# For loops

vec1 <- c(1, 2, 3, 4, 5)

# create a second vector as numeric and the same length as the first one
vec2 <- vector(mode = “numeric”, length = length(vec1))

j <- 1
for(j in vec1) {

vec2[j] <- j + 1
print(vec2)
j <- j + 1
}

print(vec2)

# Combining conditional statements with a For loop

vec2 <- c(0, 0, 0, 0, 0)
i <- 1

for(i in vec1) {
# if i is divisible by 2 (even number)
if(i %% 2 == 0) {

vec2[i] <- i + 1

} else {

vec2[i] <- i + 2

}

i <- i + 1

}

print(vec2)

# Calculate a bonus for the employee based on the months employed

names <- c(“Harvey”, “Alan”, “Tony”, “Angelina”, “Whitney”)
experience.months <- c(6, 2, 10, 12, 24)
salary.annum <- c(50000, 30000, 55000, 60000, 100000)
bonus <- vector(mode = “numeric”, length = length(names))

names
experience.months
salary.annum
bonus

employee.data <- data.frame(names, experience.months, salary.annum, bonus)
employee.data
View(employee.data)

# Computing the bonus
# Iterate from 1 to number of employees in the data frame-
# you can run it individually to see the output in the console, like this:

# 1:nrow(employee.data

for(i in 1:nrow(employee.data)) {
# based on the respective experience
exp.months <- employee.data[i,2]

if(exp.months < 10) {

employee.data[i,4] <- employee.data[i,3] * 0.10

} else {

employee.data[i,4] <- employee.data[i,3] * 0.20

}

}

print(employee.data)

# Implementing Fibonacci series using the While loop, i.e.:
# a series of numbers in which each number ( Fibonacci number )
# is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc

# the initial conditions
var1 <- 1
var2 <- 1
temp <- 0
fibo.vec <- vector(mode = “numeric”, length = 9)
counter <- 3
fibo.vec[1] <- var1
fibo.vec[2] <- var2

# start the loop
while(counter < 10) {

temp <- var1 + var2
fibo.vec[counter] <- temp
var1 <- var2
var2 <- temp

counter <- counter + 1

}

length(fibo.vec)
print(fibo.vec)

# The break and repeat statement
# Breaking out of the loop without taking into account the condition of the loop
# Repeating means continually executing unless you find a break statement
# No break statement here means that we are entrapped into an infinite loop

vec1 <- c(1:10)
vec1

vec1[7] <- -1
vec1

counter <- 1
for(i in vec1) {

if(i < 0) {

break
# the values after -1 remain the same in the output
}

if(i%%2 == 0) {
counter <- counter + 1
next

}

vec1[counter] = 0
counter <- counter + 1

}

print(vec1)

vec1 <- c(1:10)
vec1

vec1[7] <- -1
vec1

counter <- 1
repeat {

if(vec1[counter] < 0) {

break

}

vec1[counter] <- vec1[counter] + 1
counter <- counter + 1

}

print(vec1)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s