Iteration and functions
Day 4 - Introduction to R for Life Sciences
What is iteration
Repeating a process to a set of values
Often automatic:
c <- a + b # Works for vectors and matrices
Reminder: value-recycling
matrix(1:4, nrow=2, byrow=TRUE) + c(70,90) →
[,1] [,2]
[1,] 71 72
[2,] 93 94 # (the recycling is column-wise!)
Aggregation
You often need to "summarize" a vector as one number:
length(), min(), max(), mean(), median(), sd(), var(), mad(), IQR(),
sum(), prod(), all(), any(), ... # (see also: summary() )
How to do this for all rows or columns of a matrix? apply() !
result <- apply(mat, dir, FUNC, ...)
mat a matrix
dir direction: 1=rows, 2=columns
FUNC a function taking a vector, producing a single value
... additional argument(s) to FUNC
apply() cont’d
> m <- matrix(1:6, nrow=2)
> m
[,1] [,2] [,3]
[1,] 1 3 5 # filled column-wise by default
[2,] 2 4 6
> apply(m, 1, sum) # row-wise sum
[1] 9 12
> apply(m, 2, prod, na.rm=TRUE) # column-wise product, ignoring NA’s
[1] 2 12 30
And also:
lapply(lst, FUNC) # iterate over list-contents
tapply(), sapply(), mapply(), …
lapply() and sapply()
Works on lists, rather than matrices
> lst <- list(a=runif(7), b=runif(2), <etc>)
$a
[1] 0.971 0.380 0.287 0.787 0.721 0.938 0.364
$b
[1] 0.609 0.658
<etc>
> lapply(lst, mean)
$a
[1] 0.635
$b
[1] 0.633
<etc>
Custom built functions
apply() can use existing functions, but they may not suffice
e.g.: number of values exceeding 2*stddev from mean
Define a function n.exceeding2sd(), then call apply() as before
v <- apply(m, 1, n.exceeding2sd)
Functions
Functions are "recipes" that automate actions
Needed for reuse and clarity
Easy and cheap
Have to be defined
Can be called
Inputs are called arguments (which can have defaults)
special argument: the triple-dot plot(x, y, …)
Functions can have local variables (try to avoid global variables)
Outputs are called return values
Full function example
Definition:
sum.of.squares <- function(x) { # argument(s)
s <- x^2 # variables x, s and tot are local
tot <- sum(s) # note: indentation!
tot # last value is returned
} # end of definition
Call:
ss <- sum.of.squares(some.vector)
ss.percolumn <- apply(data, 2, sum.of.squares)
More complex example
Definition:
n.exceeding.SDs <- function(x, n=2, na.rm=FALSE) {
m <- mean(x, na.rm=na.rm)
s <- sd(x, na.rm=na.rm)
abs.z <- abs((x - m)/s) # Z-scores, all made positive
sum( abs.z > n, na.rm=na.rm) # last value is returned
}
Call:
n <- n.exceeding.SDs(some.vector)
outliers.per.column <- apply(data, 2, n.exceeding.sds)

More Related Content

PDF
Day 4b iteration and functions for-loops.pptx
PDF
Day 1c access, select ordering copy.pptx
PDF
11 1. multi-dimensional array eng
PDF
Cheat sheet python3
PDF
Dplyr and Plyr
PDF
10. Getting Spatial
 
PDF
Python3 cheatsheet
PPTX
Sharbani bhattacharya sacta 2014
Day 4b iteration and functions for-loops.pptx
Day 1c access, select ordering copy.pptx
11 1. multi-dimensional array eng
Cheat sheet python3
Dplyr and Plyr
10. Getting Spatial
 
Python3 cheatsheet
Sharbani bhattacharya sacta 2014

What's hot (20)

PDF
NumPy Refresher
PDF
Numpy tutorial(final) 20160303
PDF
11. Linear Models
 
DOCX
CLUSTERGRAM
PDF
Python For Data Science Cheat Sheet
PDF
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
PDF
Numpy python cheat_sheet
DOCX
Basic Calculus in R.
PDF
Data Clustering with R
PDF
C Language Lecture 10
DOCX
Advanced Data Visualization in R- Somes Examples.
PDF
13. Cubist
 
PPTX
130717666736980000
PDF
Python_ 3 CheatSheet
PDF
Clustering and Visualisation using R programming
PDF
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
PDF
Python 2.5 reference card (2009)
PDF
Introduction to NumPy for Machine Learning Programmers
PDF
6. Vectors – Data Frames
 
PDF
Scikit-learn Cheatsheet-Python
NumPy Refresher
Numpy tutorial(final) 20160303
11. Linear Models
 
CLUSTERGRAM
Python For Data Science Cheat Sheet
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
Numpy python cheat_sheet
Basic Calculus in R.
Data Clustering with R
C Language Lecture 10
Advanced Data Visualization in R- Somes Examples.
13. Cubist
 
130717666736980000
Python_ 3 CheatSheet
Clustering and Visualisation using R programming
Some R Examples[R table and Graphics] -Advanced Data Visualization in R (Some...
Python 2.5 reference card (2009)
Introduction to NumPy for Machine Learning Programmers
6. Vectors – Data Frames
 
Scikit-learn Cheatsheet-Python
Ad

Similar to Day 4a iteration and functions.pptx (20)

PDF
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
PDF
Introducción a Elixir
PDF
R code for data manipulation
PDF
R code for data manipulation
PDF
Spark workshop
PDF
Groovy collection api
PDF
Parallel R in snow (english after 2nd slide)
PDF
Basic R Data Manipulation
PDF
Functional programming in ruby
PDF
Clojure functions midje
PDF
The Ring programming language version 1.3 book - Part 16 of 88
PDF
Effective Numerical Computation in NumPy and SciPy
PDF
Introduction to ad-3.4, an automatic differentiation library in Haskell
PDF
Introduction to ad-3.4, an automatic differentiation library in Haskell
PDF
Arrays and function basic c programming notes
PPT
lecture7.ppt
PPT
its arrays ppt for first year students .
PPT
Learn Matlab
PDF
[1062BPY12001] Data analysis with R / week 2
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Introducción a Elixir
R code for data manipulation
R code for data manipulation
Spark workshop
Groovy collection api
Parallel R in snow (english after 2nd slide)
Basic R Data Manipulation
Functional programming in ruby
Clojure functions midje
The Ring programming language version 1.3 book - Part 16 of 88
Effective Numerical Computation in NumPy and SciPy
Introduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in Haskell
Arrays and function basic c programming notes
lecture7.ppt
its arrays ppt for first year students .
Learn Matlab
[1062BPY12001] Data analysis with R / week 2
Ad

More from Adrien Melquiond (9)

PPTX
Day 1a welcome introduction
PDF
R course ggplot2
PDF
Day 5b statistical functions.pptx
PDF
Day 5a iteration and functions if().pptx
PDF
Day 3 plotting.pptx
PDF
Day 2b i/o.pptx
PDF
Day 2 repeats.pptx
PDF
Day 1d R structures & objects: matrices and data frames.pptx
PDF
Day 1b R structures objects.pptx
Day 1a welcome introduction
R course ggplot2
Day 5b statistical functions.pptx
Day 5a iteration and functions if().pptx
Day 3 plotting.pptx
Day 2b i/o.pptx
Day 2 repeats.pptx
Day 1d R structures & objects: matrices and data frames.pptx
Day 1b R structures objects.pptx

Recently uploaded (20)

PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
semiconductor packaging in vlsi design fab
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
PDF
Complications of Minimal Access-Surgery.pdf
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PDF
International_Financial_Reporting_Standa.pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PPTX
Computer Architecture Input Output Memory.pptx
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PPTX
Education and Perspectives of Education.pptx
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
semiconductor packaging in vlsi design fab
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
AI-driven educational solutions for real-life interventions in the Philippine...
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
Complications of Minimal Access-Surgery.pdf
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
International_Financial_Reporting_Standa.pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Environmental Education MCQ BD2EE - Share Source.pdf
Computer Architecture Input Output Memory.pptx
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Race Reva University – Shaping Future Leaders in Artificial Intelligence
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
Education and Perspectives of Education.pptx

Day 4a iteration and functions.pptx

  • 1. Iteration and functions Day 4 - Introduction to R for Life Sciences
  • 2. What is iteration Repeating a process to a set of values Often automatic: c <- a + b # Works for vectors and matrices Reminder: value-recycling matrix(1:4, nrow=2, byrow=TRUE) + c(70,90) → [,1] [,2] [1,] 71 72 [2,] 93 94 # (the recycling is column-wise!)
  • 3. Aggregation You often need to "summarize" a vector as one number: length(), min(), max(), mean(), median(), sd(), var(), mad(), IQR(), sum(), prod(), all(), any(), ... # (see also: summary() ) How to do this for all rows or columns of a matrix? apply() ! result <- apply(mat, dir, FUNC, ...) mat a matrix dir direction: 1=rows, 2=columns FUNC a function taking a vector, producing a single value ... additional argument(s) to FUNC
  • 4. apply() cont’d > m <- matrix(1:6, nrow=2) > m [,1] [,2] [,3] [1,] 1 3 5 # filled column-wise by default [2,] 2 4 6 > apply(m, 1, sum) # row-wise sum [1] 9 12 > apply(m, 2, prod, na.rm=TRUE) # column-wise product, ignoring NA’s [1] 2 12 30 And also: lapply(lst, FUNC) # iterate over list-contents tapply(), sapply(), mapply(), …
  • 5. lapply() and sapply() Works on lists, rather than matrices > lst <- list(a=runif(7), b=runif(2), <etc>) $a [1] 0.971 0.380 0.287 0.787 0.721 0.938 0.364 $b [1] 0.609 0.658 <etc> > lapply(lst, mean) $a [1] 0.635 $b [1] 0.633 <etc>
  • 6. Custom built functions apply() can use existing functions, but they may not suffice e.g.: number of values exceeding 2*stddev from mean Define a function n.exceeding2sd(), then call apply() as before v <- apply(m, 1, n.exceeding2sd)
  • 7. Functions Functions are "recipes" that automate actions Needed for reuse and clarity Easy and cheap Have to be defined Can be called Inputs are called arguments (which can have defaults) special argument: the triple-dot plot(x, y, …) Functions can have local variables (try to avoid global variables) Outputs are called return values
  • 8. Full function example Definition: sum.of.squares <- function(x) { # argument(s) s <- x^2 # variables x, s and tot are local tot <- sum(s) # note: indentation! tot # last value is returned } # end of definition Call: ss <- sum.of.squares(some.vector) ss.percolumn <- apply(data, 2, sum.of.squares)
  • 9. More complex example Definition: n.exceeding.SDs <- function(x, n=2, na.rm=FALSE) { m <- mean(x, na.rm=na.rm) s <- sd(x, na.rm=na.rm) abs.z <- abs((x - m)/s) # Z-scores, all made positive sum( abs.z > n, na.rm=na.rm) # last value is returned } Call: n <- n.exceeding.SDs(some.vector) outliers.per.column <- apply(data, 2, n.exceeding.sds)