An introduction to R: Organisation and Basics of Algorithmics emie - - PowerPoint PPT Presentation

an introduction to r organisation and basics of
SMART_READER_LITE
LIVE PREVIEW

An introduction to R: Organisation and Basics of Algorithmics emie - - PowerPoint PPT Presentation

An introduction to R: Organisation and Basics of Algorithmics emie Becker, Sonja Grath & Dirk Metzler 1 No nbecker@bio.lmu.de - grath@bio.lmu.de Winter semester 2017-18 1 Special thanks to: Prof. Dr. Martin Hutzenthaler and Dr. Benedikt


slide-1
SLIDE 1

An introduction to R: Organisation and Basics of Algorithmics

No´ emie Becker, Sonja Grath & Dirk Metzler 1

nbecker@bio.lmu.de - grath@bio.lmu.de

Winter semester 2017-18

1Special thanks to: Prof. Dr. Martin Hutzenthaler and Dr. Benedikt

Holtmann for course development

slide-2
SLIDE 2

1

Organisation of the course

2

Basic algorithmics Algowhat? Variables Read and write Tests and logic Loops Take home message

slide-3
SLIDE 3

Organisation of the course

Where to find information about the course?

Webpage: http://evol.bio.lmu.de/ statgen/Rcourse/ws1718/ see Syllabus R-course 2018.pdf for global information All course material (presentations, scripts, exercise sheets) will be posted on the website. The handouts we will put online may contain only a summary of the contents of the slides shown in the lecture. More detailed explanations are given on the whiteboard and with practical demonstrations during the lectures. The questions in the exam refer to the whole content of the course including lectures and exercises.

slide-4
SLIDE 4

Organisation of the course

When and where?

Course from March 5 to March 16. Lectures and correction of the exercises: every morning from 9 to 12 am in B00.019 Exercise sessions: every day from 1 to 5 pm in C00.005, G00.037 or D00.021 (laptop/Mac users). You can also work from home of course.

slide-5
SLIDE 5

Organisation of the course

How do I get my 3 ECTS?

Exam on March 16 at 10 am in B00.019 Recap on April 6 at 10 am - register before March 25. You are allowed to bring a two-sided A4 ”formula” sheet in your own handwriting. Attendance is not mandatory but we strongly recommend to attend both lectures and exercise sessions to learn efficiently. The most efficient way to learn programming is to program.

slide-6
SLIDE 6

Organisation of the course

Course outline - Week 1

Monday March 5: Bascis of algorithmics Tuesday March 6: Getting started with R Wednesday March 7: Data types and structure Thursday March 8: Reading and writing data Friday March 9: Manipulating datasets

slide-7
SLIDE 7

Organisation of the course

Course outline - Week 2

Monday March 12: Plots Tuesday March 13: Programming in R Wednesday March 14: Programming in R (continued) Thursday March 15: Basic statistics with R Friday March 16: Exam

slide-8
SLIDE 8

Basic algorithmics Algowhat?

Contents

1

Organisation of the course

2

Basic algorithmics Algowhat? Variables Read and write Tests and logic Loops Take home message

slide-9
SLIDE 9

Basic algorithmics Algowhat?

Aknowledgment

For this lecture I would like to thank Christophe Darmangeat from Universit´ e Paris-Diderot for his online material (in French).

slide-10
SLIDE 10

Basic algorithmics Algowhat?

Why are computers binary?

Who has experience with programming?

slide-11
SLIDE 11

Basic algorithmics Algowhat?

Why are computers binary?

Who has experience with programming? Computers can treat binary information only. Can you cite examples of binary variables?

slide-12
SLIDE 12

Basic algorithmics Algowhat?

Why are computers binary?

Who has experience with programming? Computers can treat binary information only. Can you cite examples of binary variables? The memory of the computers is made of electronic components that can be charged or uncharged. This is the reason why they register information as binary variables symbolized by humans as 0 and 1.

slide-13
SLIDE 13

Basic algorithmics Algowhat?

Previous experience with algorithms

Who has already executed an algorithm?

slide-14
SLIDE 14

Basic algorithmics Algowhat?

Previous experience with algorithms

Who has already executed an algorithm? Examples: Follow a recipe from a book to cook. Decipher instructions to configurate a device. Assemble ikea furniture following the instructions.

slide-15
SLIDE 15

Basic algorithmics Algowhat?

Previous experience with algorithms

Who has already executed an algorithm? Examples: Follow a recipe from a book to cook. Decipher instructions to configurate a device. Assemble ikea furniture following the instructions. Who has already written an algorithm?

slide-16
SLIDE 16

Basic algorithmics Algowhat?

Previous experience with algorithms

Who has already executed an algorithm? Examples: Follow a recipe from a book to cook. Decipher instructions to configurate a device. Assemble ikea furniture following the instructions. Who has already written an algorithm? Examples: Indicate the way to the English Garden to a lost tourist. Prepare a treasure hunt for the birthday party of your little sibling. Write instructions for your grandma / grandpa / parents on how to use the printer / email / dvd player.

slide-17
SLIDE 17

Basic algorithmics Algowhat?

Definition

An algorithm is a list of instructions that, upon correct execution, lead to a wanted result.

slide-18
SLIDE 18

Basic algorithmics Algowhat?

Definition

An algorithm is a list of instructions that, upon correct execution, lead to a wanted result. To be useful, the algorithm shall contain only instructions understandable by who has to execute it (think again of some of

  • ur examples above).

But in our case, it is easier as computers are all as stupid and do not have cultural background etc...

slide-19
SLIDE 19

Basic algorithmics Algowhat?

Algorithmics and Programming

The algorithm is independent of the specific programming

  • language. It is the logic structure of the program.

Have a precise idea of instructions the program should contain before starting to type the code in the chosen language. The algorithm can be written in pseudo-code and later translated in the chosen language to be executed.

slide-20
SLIDE 20

Basic algorithmics Variables

Contents

1

Organisation of the course

2

Basic algorithmics Algowhat? Variables Read and write Tests and logic Loops Take home message

slide-21
SLIDE 21

Basic algorithmics Variables

Variables what for?

Variables are made to store information given by the user result of the program

slide-22
SLIDE 22

Basic algorithmics Variables

Variables what for?

Variables are made to store information given by the user result of the program In practice: computer assigns space in memory for information computer assigns a label to this space with a binary adress

slide-23
SLIDE 23

Basic algorithmics Variables

Variable declaration

Many languages require declaration of variables: usually at the beginning of the program need to specify the type of variable to allocate enough memory (ex: integer vs decimal)

slide-24
SLIDE 24

Basic algorithmics Variables

Variable declaration

Many languages require declaration of variables: usually at the beginning of the program need to specify the type of variable to allocate enough memory (ex: integer vs decimal) In R there is no need to declare the variables. The memory is allocated the first time the variable is assigned a value. We also do not need to define a priori the type of variable (easier for user but less effective in terms of memory).

slide-25
SLIDE 25

Basic algorithmics Variables

Types of variables

The space allocated in memory depends on the type. numerical: integer, decimal character/string boolean = binary variable

slide-26
SLIDE 26

Basic algorithmics Variables

Affectation

Attribute a value to the variable. Value must belong to defined type (not in R).

slide-27
SLIDE 27

Basic algorithmics Variables

Affectation

Attribute a value to the variable. Value must belong to defined type (not in R). Can be written in pseudo-code as: Var1 <- 24 (in pseudo-code but also in R)

slide-28
SLIDE 28

Basic algorithmics Variables

Affectation

Attribute a value to the variable. Value must belong to defined type (not in R). Can be written in pseudo-code as: Var1 <- 24 (in pseudo-code but also in R) Can also affect the value of another variable: Var2 <- Var1 Var2 <- Var1 + 4

slide-29
SLIDE 29

Basic algorithmics Variables

Affectation

Attribute a value to the variable. Value must belong to defined type (not in R). Can be written in pseudo-code as: Var1 <- 24 (in pseudo-code but also in R) Can also affect the value of another variable: Var2 <- Var1 Var2 <- Var1 + 4 The order of the instructions plays a role of course: Begin A <- 2 A <- 25 End Begin A <- 25 A <- 2 End What is the value of A?

slide-30
SLIDE 30

Basic algorithmics Variables

Operators

An operator is a sign linking two values to produce a result.

slide-31
SLIDE 31

Basic algorithmics Variables

Operators

An operator is a sign linking two values to produce a result. Possible operators depend on the type of variable numeric: +, -, *, /, ˆ text: & to concatenate (in pseudo-code not in R) boolean: and (& in R), or (| in R), no (! in R)

slide-32
SLIDE 32

Basic algorithmics Read and write

Contents

1

Organisation of the course

2

Basic algorithmics Algowhat? Variables Read and write Tests and logic Loops Take home message

slide-33
SLIDE 33

Basic algorithmics Read and write

Read and write

Necessary to communicate with the user.

slide-34
SLIDE 34

Basic algorithmics Read and write

Read and write

Necessary to communicate with the user. Write (for the computer): prompt or save something (result

  • r question to the user)

Read: read value given by the user or from file Example: Write "Please enter your name" Read Name

slide-35
SLIDE 35

Basic algorithmics Tests and logic

Contents

1

Organisation of the course

2

Basic algorithmics Algowhat? Variables Read and write Tests and logic Loops Take home message

slide-36
SLIDE 36

Basic algorithmics Tests and logic

Motivating example

Now we can ask info from the user, save it into variables, make

  • perations on variables and print results.

But the algorithm must be flexible and allow for different options. Example: print in black and white or in colors? If your doc has colors: tick colors else: tick black and white.

slide-37
SLIDE 37

Basic algorithmics Tests and logic

Structure of a test

If BooleanVariable then Instructions End of If

slide-38
SLIDE 38

Basic algorithmics Tests and logic

Structure of a test

If BooleanVariable then Instructions End of If If BooleanVariable then Instructions Else then Instructions 2 End of If

slide-39
SLIDE 39

Basic algorithmics Tests and logic

Structure of a test

If BooleanVariable then Instructions End of If If BooleanVariable then Instructions Else then Instructions 2 End of If Boolean = Expression with value TRUE or FALSE Can be a variable or a condition.

slide-40
SLIDE 40

Basic algorithmics Tests and logic

Structure of a test

If BooleanVariable then Instructions End of If If BooleanVariable then Instructions Else then Instructions 2 End of If Boolean = Expression with value TRUE or FALSE Can be a variable or a condition. What would be the pseudo-code for the example above?

slide-41
SLIDE 41

Basic algorithmics Tests and logic

Condition

A condition is a comparison. Value1 + comparison operator + Value2

  • perators: =, =, ≥, ≤, >, <

The answer is always TRUE or FALSE (boolean variable). Examples: "t" < "w" a ≥ 3 b = 3 in R ==

slide-42
SLIDE 42

Basic algorithmics Tests and logic

Composed condition

How to express 5 ≤ x ≤ 7.

slide-43
SLIDE 43

Basic algorithmics Tests and logic

Composed condition

How to express 5 ≤ x ≤ 7. AND: means both conditions must be true (& in R)

slide-44
SLIDE 44

Basic algorithmics Tests and logic

Composed condition

How to express 5 ≤ x ≤ 7. AND: means both conditions must be true (& in R) OR: means one or both are ture or at least one is true (| in R)

slide-45
SLIDE 45

Basic algorithmics Tests and logic

Composed condition

How to express 5 ≤ x ≤ 7. AND: means both conditions must be true (& in R) OR: means one or both are ture or at least one is true (| in R) NO: if A ist TRUE then NO(A) is FALSE (! in R)

slide-46
SLIDE 46

Basic algorithmics Tests and logic

Composed condition

How to express 5 ≤ x ≤ 7. AND: means both conditions must be true (& in R) OR: means one or both are ture or at least one is true (| in R) NO: if A ist TRUE then NO(A) is FALSE (! in R) Example: ”Document has colors AND you want to print the colors.” Example: ”Document has colors AND NO(you want to print the colors).”

slide-47
SLIDE 47

Basic algorithmics Tests and logic

Chained tests

Example:

Begin Write "Enter water temperature:" Read Temp If Temp ≤ 0 then Write "This is ice" End of If If Temp > 0 AND Temp < 100 then Write "This is liquid" End of If If Temp ≥ 100 then Write "This is vapor" End of If End

slide-48
SLIDE 48

Basic algorithmics Tests and logic

Chained tests

Example:

Begin Write "Enter water temperature:" Read Temp If Temp ≤ 0 then Write "This is ice" End of If If Temp > 0 AND Temp < 100 then Write "This is liquid" End of If If Temp ≥ 100 then Write "This is vapor" End of If End Begin Write "Enter water temperature:" Read Temp If Temp ≤ 0 then Write "This is ice" Else then If Temp < 100 then Write "This is liquid" Else then Write "This is vapor" End of If End of If End

slide-49
SLIDE 49

Basic algorithmics Tests and logic

Chained tests

Example:

Begin Write "Enter water temperature:" Read Temp If Temp ≤ 0 then Write "This is ice" End of If If Temp > 0 AND Temp < 100 then Write "This is liquid" End of If If Temp ≥ 100 then Write "This is vapor" End of If End Begin Write "Enter water temperature:" Read Temp If Temp ≤ 0 then Write "This is ice" Else then If Temp < 100 then Write "This is liquid" Else then Write "This is vapor" End of If End of If End

What can be a good graphical representation for this?

slide-50
SLIDE 50

Basic algorithmics Tests and logic

A bit more about logic

Parentheses are important: (A and B) or C is different from A and (B or C).

slide-51
SLIDE 51

Basic algorithmics Tests and logic

A bit more about logic

Parentheses are important: (A and B) or C is different from A and (B or C). AND vs OR If it is too hot AND it is not raining then Open the window Else then Do not open the window

slide-52
SLIDE 52

Basic algorithmics Tests and logic

A bit more about logic

Parentheses are important: (A and B) or C is different from A and (B or C). AND vs OR If it is too hot AND it is not raining then Open the window Else then Do not open the window Can also be written as: If it is not too hot OR it is raining then Do not open the window Else then Open the window

slide-53
SLIDE 53

Basic algorithmics Loops

Contents

1

Organisation of the course

2

Basic algorithmics Algowhat? Variables Read and write Tests and logic Loops Take home message

slide-54
SLIDE 54

Basic algorithmics Loops

Motivation example

Write "Do you like this course? (Y/N) Read Answer If Answer = Y then Write "Me too" Else then Write "What a pity!" End If What happens if the user answers something stupid?

slide-55
SLIDE 55

Basic algorithmics Loops

Motivation example

Write "Do you like this course? (Y/N) Read Answer If Answer = Y then Write "Me too" Else then Write "What a pity!" End If What happens if the user answers something stupid? ”What a pity!”

slide-56
SLIDE 56

Basic algorithmics Loops

1st solution: Test

You want to check if the answer is correct. Write "Do you like this course? (Y/N) Read Answer If Answer NO Y and Answer NO N then Write "Please answer by Y or N" Read Answer End If If Answer = Y then Write "Me too" Else then Write "What a pity!" End If

slide-57
SLIDE 57

Basic algorithmics Loops

1st solution: Test

You want to check if the answer is correct. Write "Do you like this course? (Y/N) Read Answer If Answer NO Y and Answer NO N then Write "Please answer by Y or N" Read Answer End If If Answer = Y then Write "Me too" Else then Write "What a pity!" End If What happens if the user answers something stupid twice?

slide-58
SLIDE 58

Basic algorithmics Loops

1st solution: Test

You want to check if the answer is correct. Write "Do you like this course? (Y/N) Read Answer If Answer NO Y and Answer NO N then Write "Please answer by Y or N" Read Answer End If If Answer = Y then Write "Me too" Else then Write "What a pity!" End If What happens if the user answers something stupid twice? ”What a pity!”

slide-59
SLIDE 59

Basic algorithmics Loops

Real solution: Loop

You want to check if the answer is correct. Write "Do you like this course? (Y/N) Read Answer While Answer NO Y and Answer NO N then Write "Please answer by Y or N" Read Answer End While If Answer = Y then Write "Me too" Else then Write "What a pity!" End If While loop means that you repeat instruction as long as the condition is TRUE.

slide-60
SLIDE 60

Basic algorithmics Loops

Types of loops

There are two types of loop. No specific number of iterations While. Specific number of iterations For

slide-61
SLIDE 61

Basic algorithmics Loops

Types of loops

There are two types of loop. No specific number of iterations While. Specific number of iterations For Structure of the For loop: For i varying from 1 to 70 then Hello Name(student i)! End For

slide-62
SLIDE 62

Basic algorithmics Loops

Types of loops

There are two types of loop. No specific number of iterations While. Specific number of iterations For Structure of the For loop: For i varying from 1 to 70 then Hello Name(student i)! End For You can specify that i should vary 2 by 2 or any other. In R you simply give a vector of values for i. Of course you can name the variable something else as i.

slide-63
SLIDE 63

Basic algorithmics Loops

More loops

You can also imbed a loop into another loop. Many errors due to the name of the counter in For loops. More about that when we will use these notions in R next week.

slide-64
SLIDE 64

Basic algorithmics Take home message

Contents

1

Organisation of the course

2

Basic algorithmics Algowhat? Variables Read and write Tests and logic Loops Take home message

slide-65
SLIDE 65

Basic algorithmics Take home message

Take home message

An algorithm is a list of instructions to execute. You need to have the algorithm in mind before starting to write it as a script in the chosen language.