•R is a full fledged programming language.
It can do loops and conditionals.
i <- runif(1,0,100)
if(i <= 50)
{
print(‘i is pretty small’)
}
Control Structures
for(i in 1:100)
{
print(i)
}
Slide 2
Slide 2 text
•A loop will be executed over and over.
Control Structures
fruits<-
c('apple','orange','banana','pear')
for(fruit in fruits)
{
print(fruit)
}
Slide 3
Slide 3 text
•Combining loops with conditionals can be
very powerful.
Control Structures
fruits<-
c('apple','orange','banana','pear')
for(fruit in fruits)
{
print(fruit)
if(fruit == 'banana')
print(paste(fruit,'rama!',sep='')
}
Slide 4
Slide 4 text
Challenge
1) Print out all the numbers between 1 and
100, but if the number is a multiple of 3,
print ‘fizz’ instead.
2) Extend your code so that for multiples of
5, it prints out ‘buzz’.
HINT: To get the remainder of integer
division, use %%. Ex:
4
9%%5