[emaillocker]Below are the solutions to these exercises on conditional execution.

# Exercise 1 x <- -10 abs <- x if (x < 0) { abs = -x } cat("The absolute value of ", x, " is ", abs , "\n" )

## The absolute value of -10 is 10

# Exercise 2 x <- 16 y <- ifelse(x >= 0, x, NA) cat("The square root of", x, "is", sqrt(y))

## The square root of 16 is 4

# Exercise 3 x <- c(10, 1) if(x[1] > x[2]) { cat("Max value is", x[1], "\n") } else cat("Max value is", x[2], "\n")

## Max value is 10

# Exercise 4 x <- c(10, 11, 12) grow <- FALSE ifelse ( ( (x[1] < x[3] & x[1] < x[2]) & x[2] < x[3]), grow <- TRUE, grow)

## [1] TRUE

if (grow){ cat ("Increasing strictly \n") } else cat ("Not increasing strictly \n")

## Increasing strictly

# Exercise 5 x <- c(20, 10, 1) if (x[1] > x[2] & x[1] > x[3] ) { cat (x[1], "\n" ) } else if (x[2] > x[3] ) { cat (x[2] , "\n" ) } else { cat (x[3] , "\n" ) }

## 20

# Exercise 6 x <- c(-100, 10, 20, 30, 50, 51, 52, 53, 54, 55) counter <- 0 mean <- mean(x) for (i in 1:length(x)){ if(x[i] > mean){ counter <- counter +1 } } cat("The number of values that are bigger than the mean is", counter, "\n")

## The number of values that are bigger than the mean is 7

# Exercise 7 x <- c(30, 120, 100) if (x[1] > x[2]){ fir <- x[1] sec <- x[2] } else { fir <- x[2] sec <- x[1] } if ( x[3] > fir & x[3] > sec ) { thi <- sec sec <- fir fir <- x[3] } else if ( x[3] < fir & x[3] < sec ) { thi <- x[3] } else { thi <- sec sec <- x[3] } cat (fir, sec, thi, "\n")

## 120 100 30

[/emaillocker]

Kiran Shetty says

can we use the sort function for exercise #7, simply?

Sergio says

I guess the idea here is to code using only if (else).

mean() was allowed only to not overcomplicate the if/else structure.