Nathan Byers
April 21, 2014
Now that we've covered the basics and some intermediate material, we'll get into more advanced topics
for loopslapply() functionmatrix() function as an example?matrix mean() function in R, we're going to call our function myMean()<-myMean <- function(){
}
Since the function will require a vector, we put a variable name for that in the parenthesis
myMean <- function(vector){
}
length() function to get that For example, here we use length() to find the number of values in this vector
x <- c(1, 4, 3, 8, 2)
length(x)
[1] 5
sum() functionsum(x)
[1] 18
Now we have all of the necessary pieces to write our calculation in the curly braces
myMean <- function(vector){
sum.of.vector <- sum(vector)
number.of.values <- length(vector)
average <- sum.of.vector/number.of.values
average
}
Here we use the x variable we've already created
myMean(x)
[1] 3.6
x.mean <- myMean(x)
x.mean
[1] 3.6
apply() functionsfor() in R to repeat a sequence of evaluations for a set number of timesfor(var in seq)
var is the current iteration, and seq is the sequence of numbers over which the loop iteratesLet's say we have three vectors, and we want to use our myMean() function on all of them
x <- c(1, 4, 3, 8, 2)
y <- c(10, 43, 8, 15, 29, -4)
z <- c(1.3, 5.6, 7)
seq would be the vector c(1, 2, 3) or equivalently 1:3, because we want to run one function 3 timesvar equal to 1var <- 1
for(var in 1:3){
}
1, 2, and 3list data typevect.list <- list(x, y, z)
vect.list
[[1]]
[1] 1 4 3 8 2
[[2]]
[1] 10 43 8 15 29 -4
[[3]]
[1] 1.3 5.6 7.0
1, 2, and 3 in the double bracketsthe.means <- c()the.means <- c()
var <- 1
for(var in 1:3){
the.means[var] <- myMean(vect.list[[var]])
var <- var + 1
}
the.means is filled with the first mean value from our function, which calculates the mean of the first vector in the list1 to varfor loop runs what is inside the curly braces again, but the second time var has the value 2 and myMean calculates the mean of the second vector in the listOur resulting vector of mean values is
the.means
[1] 3.600 16.833 4.633
lapply() functionlapply() is one of the (in)famous apply() functions (run ?apply and ?lapply() to see descriptions of the many related functions)lapply() is a simple function which takes just two argumentsapply functions are so different from the way most loops are done in other languages, it's sometimes hard to think about your programming problem in apply-like termslapply aslapply(X, FUN, ...)
FUN) to a list of data objects (X)myMean() to the list vect.listlapply(vect.list, myMean)
[[1]]
[1] 3.6
[[2]]
[1] 16.83
[[3]]
[1] 4.633
class() function to see what kind of data type the output islapplyed.means <- lapply(vect.list, myMean)
class(lapplyed.means)
[1] "list"
unlist()vectored.means <- unlist(lapplyed.means)
class(vectored.means)
[1] "numeric"
vectored.means
[1] 3.600 16.833 4.633