10-11 Flashcards
What does lapply and sapply give back?
lapply() always returns a list, whereas sapply() attempts to simplify the result.
What does the s in sapply() stand for and what does saplly give back.
simplify
if the result is a list where every element is of length one, then sapply() returns a vector. If the result is a list where every element is a vector of the same length (>1), sapply() returns a matrix. If sapply() can’t figure things out, then it just returns a list, no different from what lapply() would give you
What does this mean? flags[, 11:17]
extracting all rows but only columns 11-17
How do lapply and sapply loop?
first argument is the whole object. The loop by default over each column and use the function given as the second argument to apply this function to each column wise.
function that returns a vector with all duplicate elements removed
unique()
advantage of vapply to sapply
Whereas sapply() tries to ‘guess’ the correct format of the result, vapply() allows you to specify it explicitly. If the result doesn’t match the format you specify, vapply() will throw an error, causing the operation to stop.
You might think of vapply() as being ‘safer’ than sapply(), since it requires you to specify the format of the output in advance, instead of just allowing R to ‘guess’ what you wanted.
What is tapply() good for
To understand clearly lets imagine you have height of 1000 people ( 500 male and 500 females), and you want to know the average height of males and females from this sample data. To deal with this problem you can group height by the gender, height of 500 males, and height of 500 females, and later calculate the average height for males and females.
How is tapply used?
tapply(medical.example$age,medical.example$treatment, mean) … first two are groups to split at, third is the function wanted
Explain split()
split divides the data in the vector x into the groups defined by f
split(x, f, drop = FALSE, …)
x: vector, data frame
f: indices
drop: discard non existing levels or not