Software Tools for Earth and Environmental Science

– 9th WEEK –

Emir Toker

18/11/2020

R Promgramming - Part 2

  • Syllabus and Book

  • Practice - R Programming ( Conditions )

  • R Programming - repeat Loops

  • R Programming - while Loops

  • R Programming - for Loops

  • R Programming - apply groups

QUIZ

  • Next Week

Syllabus and Book

Syllabus

Extended Syllabus PDF

Book

PDF - (179 - 185)

R Language - Repeat

Repeat - R Programming (Conditions)

  • Comparison Operators
    • equal (==)
    • not equal (!=)
    • greater or equal to (>=)
    • less or equal to (<=)
  • Logical Operators
    • the and operator (&)
    • the or operator (|)
    • the not operator (!)
  • if (Stand-Alone) Statement
  • else Statement
  • else if Statement

R Language - Repeat

LINK

R Programming - Conditions

R Programming - Conditions

  • Comparison Operators
    • equal (==)
    • not equal (!=)
    • greater or equal to (>=)
    • lower or equal to (<=)
  • Logical Operators
    • the and operator (&)
    • the or operator (|)
    • the not operator (!)
  • if (Stand-Alone) Statement
  • else Statement
  • ifelse Statement
TRUE & TRUE  
TRUE & FALSE
TRUE | FALSE 
!TRUE

2 == 3
5 < 6

c(1,4) >= 6

9 != 8

5 < 6 & 9 != 8
score <- 80
exam_no <- 2

score >= 75 | exam_no == 1

score>=75 & score<90 | exam_no==1

Repeat - R Programming (Conditions)

Practice I - R Programming (Conditions)

Problem : You want to enjoy at the weekend (“Saturday” or “Sunday”), and let’s say the day is;

day <- "Friday"

What do you gonna do if it is Friday.

if (...) {
  print('Weekend : Sorry. Lockdown.')
} else {
  print('Do some work.')
}

Practice I - R Programming (Conditions)

Problem : You want to enjoy at the weekend (“Saturday” or “Sunday”), and let’s say the day is;

day <- "Friday"

What do you gonna do if it is Friday.

if ( day... | day... ) {
  print('Weekend : Sorry. Lockdown.')
} else {
  print('Do some work.')
}

Practice I - R Programming (Conditions)

ANSWER : You want to enjoy, and let’s say the day is;

day <- "Friday"

It is okay, you can fun if it is weekend.

if (day == 'Saturday' | day == 'Sunday') {
  print('Weekend : Sorry. Lockdown.')
} else {
  print('Do some work.')
}
## [1] "Do some work."

Practice II - R Programming (Conditions)

Problem : You want to go out and your question is

“Should I take an umbrella?”

  • Note : There are two variables in your code,
    • “sky” (character)
    • “high_chance_of_rain” (logical)
  • Check: if “sky” is equal to “cloudy” and, is the “high_chance_of_rain” TRUE or FALSE
  • If both are true, the code should print: “Take umbrella!”
  • Otherwise, the code should print: “No need for umbrella!”
  • Based on the condition, what is the answer?
RADIO: The sky is cloudy and the chance of rain is high.

Practice II - R Programming (Conditions)

Your conditions, for two variables

# you want to go out and your question is "Should I take an umbrella?"

sky <- 

high_chance_of_rain <- 

Practice II - R Programming (Conditions)

Your conditions, for two variables

# you want to go out and your question is "Should I take an umbrella?"

sky <- "cloudy"

high_chance_of_rain <- TRUE
  • Check: if “sky” is equal to “cloudy” and, is the “high_chance_of_rain” TRUE or FALSE
  • If both are true, the code should print: “Take umbrella!”
  • Otherwise, the code should print: “No need for umbrella!”

Practice II - R Programming (Conditions)

# you want to go out and your question is "Should I take an umbrella?"

sky <- "cloudy"

high_chance_of_rain <- TRUE
if (...) {
  print("...")
} else { 
  print("...")
  }

Practice II - R Programming (Conditions)

# you want to go out and your question is "Should I take an umbrella?"

sky <- "cloudy"

high_chance_of_rain <- TRUE
if (...) {
  print("Take umbrella!")
} else { 
  print("No need for umbrella!")
  }

Practice II - R Programming (Conditions)

# you want to go out and your question is "Should I take an umbrella?"

sky <- "cloudy"

high_chance_of_rain <- TRUE
if ( sky...     &      high_chance_of_rain... ) {
  print("Take umbrella!")
} else { 
  print("No need for umbrella!")
  }

Practice II - R Programming (Conditions)

# you want to go out and your question is "Should I take an umbrella?"

sky <- "cloudy"

high_chance_of_rain <- TRUE

if (sky == "cloudy" & high_chance_of_rain == TRUE) {
  print("Take umbrella!")
} else { 
  print("No need for umbrella!")
  }
## [1] "Take umbrella!"

R Programming - Loops

R Programming - Loops

Loops are R’s method for repeating a task, which makes them a useful tool for programming simulations.

R Programming - repeat Loops

R Programming - repeat Loops

repeat
{
  message("Happy Free Day!")
  break
}

R Programming - repeat Loops

I have 3 COINS and I want to play the game. (So, at first I have 0 GAME)

When I started the repeat-loop I want to
  - Reduce (decrease) 1 Coin for each game
  - Enhance (increase) 1 Game for each game

R Programming - repeat Loops

I have 3 COINS and I want to play the game. (So, at first I have 0 GAME)

When I spend my all coins, I want to stop playing game

R Programming - repeat Loops

## Game =1 Nice try, play again
## Game =2 Nice try, play again
## Game =3 Nice try, play again

R Programming - repeat Loops

What if, there is no any break command ?!

# Statements previous to repeat loop
coins <- 3
game <- 0

repeat {
# block of statements
  game <- game + 1
  coins <- coins -1
  
  message("Game =",game, " Nice try, play again")

  # breaking condition
  # there is no any break command !
}
# use CTRL + C 

R Programming - while Loops

R Programming - while Loops

While loops are like backward repeat loops.

## [1] "LOOP"
## [1] "LOOP"
## [1] "LOOP"
## [1] "LOOP"
## [1] "LOOP"
## [1] 10

R Programming - while Loops

While loops are like backward repeat loops.

## Game = 1 Nice try, play again
## Game = 2 Nice try, play again
## Game = 3 Nice try, play again
## [1] 0 3

R Programming - for Loops

R Programming - for Loops

When you know exactly how many times you want the code to repeat.

## [1] 1
## [1] 2
## [1] 3
## [1] 16

R Programming - for Loops

When you know exactly how many times you want the code to repeat.

## [1] "a"
## [1] "b"
## [1] "c"
## [1] 16

loop is related with the length of the vector !

R Programming - for Loops

## just say it 1 times
## just say it 2 times

R Programming - for Loops

When you know exactly how many times you want the code to repeat.

## just say it 1 times
## just say it 2 times
## just say it apple times
## just say it banana times

LOOP is related with the length(vector)

R Programming - for Loops

##  [1] "January"   "February"  "March"     "April"     "May"       "June"     
##  [7] "July"      "August"    "September" "October"   "November"  "December"
## The month of January
## The month of February
## The month of March
## The month of April
## The month of May
## The month of June
## The month of July
## The month of August
## The month of September
## The month of October
## The month of November
## The month of December

R Programming - for Loops

for (i in 1:10) {
  if (i == 2){
    print("i don't use i=2")
    next
  }
  print(i)
}

R Programming - for Loops

## [1] 1
## [1] "i don't use i=2"
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10

R Programming - for Loops

## [1] 1
## [1] "i don't use i=2"
## [1] 3
## [1] 4
## [1] 5
## [1] 106
## [1] 7
## [1] 8
## [1] 9
## [1] 10

R Programming - apply Groups

R Programming - apply Groups

  • The apply() functions form the basis of more complex combinations and helps to perform operations with very few lines of code.
  • More specifically, the family is made up of the apply(), lapply() , sapply(), vapply(), mapply(), rapply(), and tapply() functions.
apply(X, MARGIN, FUN, ...)

# X is an array or a matrix, dim(X) must have a positive length
# MARGIN=1, it applies over rows, whereas with 
# MARGIN=2, it works over columns. Note that when you use the construct 
# MARGIN=c(1,2), it applies to both rows and columns
# FUN, which is the function that you want to apply to the data. 
apply(my_matrix, MARGIN=1, mean)

R Programming - apply

##             [,1]       [,2]        [,3]      [,4]       [,5]
## [1,]  2.02005688 -0.7510888  0.85241277 0.5159643  0.8189546
## [2,] -0.05705512  0.3836436 -0.08993873 0.4894480  0.1533690
## [3,] -0.57701045  0.4081727 -1.81256234 0.1610991 -0.6685669
## [1]  0.6912600  0.1758934 -0.4977736
## [1]  0.46199710  0.01357582 -0.35002943  0.38883714  0.10125224

R Programming - apply

##            [,1]       [,2]        [,3]         [,4]        [,5]
## [1,]  0.8610810  1.4086325 -0.06280369 -0.002602994  0.05864157
## [2,]  0.9518314 -0.7794236  0.68562154 -0.340228385 -0.78159277
## [3,] -0.8031686 -1.1291807  0.72255161  0.221269524  0.70444499
##           [,1]        [,2]        [,3]
## [1,] 0.4525897 -0.05275835 -0.05681664
## [2,] 2.2629484 -0.26379173 -0.28408318

R Programming - apply

##         col1       col2        col3
## a -0.2826586  0.6926466  0.05438504
## b  1.0349155 -1.2255437 -0.06929384
## c  1.6882855 -0.3319602  0.68032112
## d -0.4779836  0.4604776  0.52732802
## e  0.4011958 -0.1248847  0.08651011
## a b c d e 
## 1 2 2 1 2

R Programming - lapply

## [[1]]
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
## 
## [[2]]
##      [,1] [,2] [,3]
## [1,]    4    8   12
## [2,]    5    9   13
## [3,]    6   10   14
## [4,]    7   11   15
## 
## [[3]]
##      [,1] [,2]
## [1,]    8    8
## [2,]    9    9
## [3,]   10   10

R Programming - lapply

## [[1]]
## [1] 4 5 6
## 
## [[2]]
## [1]  8  9 10 11
## 
## [[3]]
## [1]  8  9 10

R Programming - lapply

## [[1]]
## [1] 1 4 7
## 
## [[2]]
## [1]  4  8 12
## 
## [[3]]
## [1] 8 8

R Programming - sapply (simplify)

## [[1]]
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
## 
## [[2]]
##      [,1] [,2] [,3]
## [1,]    4    8   12
## [2,]    5    9   13
## [3,]    6   10   14
## [4,]    7   11   15
## 
## [[3]]
##      [,1] [,2]
## [1,]    8    8
## [2,]    9    9
## [3,]   10   10
## [1] 1 4 8
##      [,1] [,2] [,3]
## [1,]    1    4    8
## [2,]    2    5    9
## [3,]    3    6   10

R Programming - Conditions

LINK

Next Week

Next Week

Conditions and Loops, DataCamp

(Optional) Practice - MidTerm Project