lecture_05

# lecture_05 - BIOS 735 Statistical Computing Michael Wu...

This preview shows pages 1–6. Sign up to view the full content.

BIOS 735: Statistical Computing Michael Wu Lecture 5: More Functional Programming and Intro to Programming Structures September 6, 2011 Michael Wu (Lecture 5) BIOS 735 September 6, 2011 1 / 17

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Functional Programming: Applying the Same Function to Vector, Matrix, List Recall: we are often interested in applying the same function to all elements of a particular data structure. Typical way to do this in many programming languages is to use loops – in R this is generally slow and should be avoided where possible (sometimes impossible) Many operations are naturally vectorized, (”+”, ” ”, etc.) If what we’re doing is more complicated or we’re not dealing with vectors, then we need to use alternative methods: I apply (rows and columns of a matrix) (Last Class) I lapply and sapply and vapply and mapply Michael Wu (Lecture 5) BIOS 735 September 6, 2011 2 / 17
Functional Programming: lapply Function lapply is commonly used to apply the same function to each element of a list or vector Simple Example: a = seq(5) f = function(foo) {return(foo+1)} lapply(a, f) lapply(a, "+", 100) f= function(x){return(rep(x, times = x+1))} lapply(a,f) lapply takes in a vector or a list as its ﬁrst variable, then a function to be applied to each element of a list. Subsequent parameters are accepted. lapply will return a list that is of the same length as whatever vector/list you applied it do. Michael Wu (Lecture 5) BIOS 735 September 6, 2011 3 / 17

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Functional Programming: lapply Examples Example: Application to Lists a = list(letter = c("a","b","c"), animals = c("cow", "duck", "penguin", "rat", "dodo"), question= c("who", "what", "where", "why")) lapply(a, length) question = as.list(a\$question) lapply(question, paste, "?", sep = "") Example: Application to Data Frames and Matrices A = matrix(1:100, ncol = 25) lapply(A, "+", 100) A = data.frame(matrix(1:100, ncol = 25)) lapply(A, median) Michael Wu (Lecture 5) BIOS 735 September 6, 2011 4 / 17
Functional Programming: sapply Function At a glance, sapply looks a lot like the lapply function. Simple Example: a = seq(5) f = function(foo) {return(foo+1)} sapply(a, f) lapply(a, f) sapply(a, "+", 100) lapply(a, "+", 100) sapply(a,rep, 3) lapply(a,rep, 3) f= function(x){return(rep(x, times = x+1))} sapply(a,f) lapply(a,f) sapply is much like lapply in that it takes in a vector/list/matrix as its ﬁrst variable, then a function to be applied to each element.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 20

lecture_05 - BIOS 735 Statistical Computing Michael Wu...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online