Software Tools for Earth and Environmental Science

– 8th WEEK –

Emir Toker

11/12/2020

R Promgramming - Part 1

  • Syllabus and Book
  • DataCamp Class
  • Homework I & II and Midterm Project
  • Questions - R Language
  • Practice - Create a Function
  • R Programming - Conditions
    • Comparison Operators
    • if Statement
    • else Statement
    • else if Statement
  • Next Week

Syllabus and Book

Syllabus

Extended Syllabus PDF

Book

PDF - (179 - 185)

R Language - Repeat

Questions - R Language

  • What are the benefits of R-Script and R-Project?
  • What are the data types and data structures in R?
  • What are the advantages of the Data Frame?
  • What is the difference between NA and NaN?

LINK

Practice - Create a Function

What is Function ?

A function is a set of statements organized together to perform a specific task

ex: mean() (arithmetic mean)

x <- c(1,2,3)
mean(x)
## [1] 2
(1+2+3) / 3
## [1] 2

Create a Function

Create a Function

Create a Function

Problem: Take a sample belonged to population, and sum

box <- 1:6                    # This is my population in a BOX
box
## [1] 1 2 3 4 5 6
samp <- sample(box, size = 2)   # This is my sample, I choose two values.
samp
## [1] 3 1
sum(samp)
## [1] 4

Create a Function

I want to collect my sample() and sum() functions and put in ONE function. I will create a new function named roll2().

name_new_function <- function( argument ) {

name_new_variable <- do_this( argument, option )
then_do_this( name_new_variable )
}
name_new_function( )    # It will work with my default argument

Create a Function

name_new_function <- function( argument ) {

name_new_variable <- do_this( argument, option )
then_do_this( name_new_variable )
}
name_new_function( )   # It will work with my default argument

in formal :

roll2 <- function(box = 1:6) {
  
samp <- sample(box, size=2) 
sum(samp)
}
roll2( )      # It will work with my default argument ( box = 1:6 )
## [1] 9

Create a Function

roll2 <- function(box = 1:6) {
  
samp <- sample(box, size=2) 
sum(samp)
}
roll2()    # It will work with my default argument ( box = 1:6 )
## [1] 7

Create a Function

You can change the default argument in every time

roll2(box = 1:10)   # It will work with argument ( 1,2,3,4,5,6,7,8,9,10 )
## [1] 17
roll2(1:10)   # It will work with argument ( 1,2,3,4,5,6,7,8,9,10 )
## [1] 14
roll2(seq(1,10,0.5))   # It will work with argument ( 1, 1.5, 2, 2.5, 3,... )
## [1] 9.5

Practice - Create a Function

You can add new options or new functions in your new function.

{ } and () are important

# Think about these functions
# mean(), print(), plot(), max(), install.packages(), help(), ...

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_name <- "math"

score >= 75 & exam_name == "math"

R Programming - Conditions

LINK

Next Week

Next Week

Check, Conditions and Loops, DataCamp