71 views

Uploaded by GCEID

Lecture 6: loops,control structures & apply family

save

- The Best Python Interview Questions & Answers 2018 - Know More!
- PercilLab4_2
- Parallel Computing — Julia Language 0.4.pdf
- LECT06.pdf
- 2016011910503600012261_1446T - PPT5 - R0
- Chapter 8
- CPET190_Lect9
- Fortran 77 Looping
- lecture_4_repetition_structures.ppt
- Maya Python
- DOT NET Basics Important
- Lesson 12 Switch
- f
- VHDL
- PLSQL for Beginners caosys
- Style Rules Checklist
- Introduction of Flowchart 3664210
- Outcome of 2018 Vice-Chancellor's Awards application
- Program-CIE Conference-2016.xlsx
- World One Health Day 2017
- R Course 2014: Lecture 8
- One Health 2016 Workshop Flyer
- Influenza Virus Research and Funding Opportunities by John Stambas
- 5th HDR Conference Program (2014)
- R Course 2014: Lecture 1
- Regular Expresssion Cheat Sheet: Lecture 3
- GCEID - Cooperative Research Centres

You are on page 1of 21

,

control structures & apply family

Simeon Lisovski

Ben Fanson

… last week … dplyr

1) introduce the grammar of data manipulation

2) table verbs

3) building sentences

4) restructuring

… last week … dplyr

ds %.%

select(treatment, growth_rate) %.%

group_by(treatment) %.%

mutate( mean_growth = mean(growth_rate) )

ds %.%

select(treatment, growth_rate) %.%

mutate( growth_rate2 = growth_rate ^2 ) %.%

filter( treatment == 't1' )

… this week …

1) loops (for, while, repeat)

2) Control structure (if… else)

3) Loop families (apply, lapply, tapply)

For Loops

for (<index> in <vector>) {

<statements>

}

the <vector> determines what value <index> will take in a loop

• The loop is performed length(vector) times

• On the n’th iteration of the loop, var takes the value vector[n]

• indexis a completely new variable and not directly related to anything

other variable

Control structures

if (<logical expression>) {

<statements>

}

Sometimes, a block of code should be executed only if a certain

condition is satisfied. In this case, the if structure can be used:

Control structures

You will often need to distinguish between several cases. In

this case, you can extend the if structure by one or more else

clauses

if (<logical expression>) {

<statements>

} else {

<statements>

}

while() and repeat() loops

while (<logical expression>) {

<statements>

}

repeat {

<statements>

if (<logical expression>) break

}

Another loop structure provided by R is

the repeat structure. This structure

repeats the commands in its body until a

break statement is reached

One of the simplest looping

structures is the while loop. Here,

the <statements> will be executed

until <logical expression> is FALSE.

Be aware of infinite loops.

apply Family

apply Family

http://adv-r.had.co.nz/Data-structures.html

apply Family

A B C

ds =

apply Family

A B C

ds =

ds %.%

mutate( D = mean(C) )

apply Family

ds %.%

mutate( D = mean(C) )

A B C

ds =

D

apply(ds$C, 2, mean)

ds$D <- apply(ds$C, 2, mean)

apply(ds$C, 2, mean)

ds$D <- apply(ds$C, 2, mean)

ds %.%

mutate( D = mean(C) )

apply Family

A B C

ds =

D

apply(ds, 2, mean)

ds$D <- apply(ds, 2, mean)

apply Family

ds %.%

mutate( D = mean(C) )

A B C

ds =

mean

median

max

min

length

as.character

as.numeric

as.factors

is.na

…

apply(ds$C, 2, mean)

ds$D <- apply(ds$C, 2, mean)

apply(ds, 2, mean)

ds$D <- apply(ds, 2, mean)

apply(ds, 1, mean)

apply Family

A B C

ds =

D

1

1

2

2

tapply(ds$C, ds$D, mean)

apply Family

A B C

lst[[1]] =

lst =

A B C

lst[[2]] =

result <- lapply(lst, mean)

result =

result[[1]] = mean(lst[[1]])

result[[2]] = mean(lst[[2]])

apply Family

A B C

lst[[1]] =

lst =

A B C

lst[[2]] =

result = c(mean(lst[[1]], mean(lst[[2]]) result <- sapply(lst, mean)

Lecture 6: Hands on Section

1) get Lecture6.R from github

- lots of data for todays ‘hands on section’ will be simulated.

Lecture 6 files

- The Best Python Interview Questions & Answers 2018 - Know More!Uploaded bysrivatsa
- PercilLab4_2Uploaded byInnie Percil
- Parallel Computing — Julia Language 0.4.pdfUploaded byskoobooks
- LECT06.pdfUploaded byAnirodh
- 2016011910503600012261_1446T - PPT5 - R0Uploaded byFarid Shadiqi
- Chapter 8Uploaded byarun13vishwakarma
- CPET190_Lect9Uploaded byLayl Zan
- Fortran 77 LoopingUploaded byrodwellhead
- lecture_4_repetition_structures.pptUploaded byKate Maiah Vasquez
- Maya PythonUploaded byRiya Gowarikar
- DOT NET Basics ImportantUploaded bydinesh
- Lesson 12 SwitchUploaded bySmita R. S.
- fUploaded byMuhammad Hamza
- VHDLUploaded byvaseem_ahmed
- PLSQL for Beginners caosysUploaded byNirav Bhatia
- Style Rules ChecklistUploaded byKinger.one
- Introduction of Flowchart 3664210Uploaded byZaile Balaba

- Outcome of 2018 Vice-Chancellor's Awards applicationUploaded byGCEID
- Program-CIE Conference-2016.xlsxUploaded byGCEID
- World One Health Day 2017Uploaded byGCEID
- R Course 2014: Lecture 8Uploaded byGCEID
- One Health 2016 Workshop FlyerUploaded byGCEID
- Influenza Virus Research and Funding Opportunities by John StambasUploaded byGCEID
- 5th HDR Conference Program (2014)Uploaded byGCEID
- R Course 2014: Lecture 1Uploaded byGCEID
- Regular Expresssion Cheat Sheet: Lecture 3Uploaded byGCEID
- GCEID - Cooperative Research CentresUploaded byGCEID