[For this exercise, first write down your answer, without using R. Then, check your answer using R.] Answers to the exercises are available here. Exercise 1 Using the following variables: x=1 i=c(1:10) For this exercise, write a for() loop that increments x by two for each i. Exercise 2 Using the following variables: x=1 y=40 […]

# loops and conditional execution

## If else sequences Exercises

Answers to the exercises are available here. Exercise 1 What is the output of: ifelse(sqrt(9)<2,sqrt(9),0) Exercise 2 What is the output of: ifelse(sqrt(100)>9,sqrt(100),0) Exercise 3 What is the output y of: x=12 if(is.numeric(x)) y=x*2 Exercise 4 What is the output x,y of: z=-1 if(z<0){x=abs(z);y=z*3} Exercise 5 What is the output y of: z=6 if(z<0) y=z*3 […]

## Scripting Loops In R

An R programmer can determine the order of processing of commands, via use of the control statements; repeat{}, while(), for(), break, and next Answers to the exercises are available here. Exercise 1 The repeat{} loop processes a block of code until the condition specified by the break statement, (that is mandatory within the repeat{} loop), […]

## Conditional execution exercises

In the exercises below we cover the basics of conditional execution. In all previous exercises, the solutions required one or more R statements that were all executed consecutively. In this series of exercises we’re going to use the if, else and ifelse functions, to execute only a subset of the R script, depending on one […]