10-11 Flashcards

1
Q

What does lapply and sapply give back?

A

lapply() always returns a list, whereas sapply() attempts to simplify the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the s in sapply() stand for and what does saplly give back.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does this mean? flags[, 11:17]

A

extracting all rows but only columns 11-17

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do lapply and sapply loop?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

function that returns a vector with all duplicate elements removed

A

unique()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

advantage of vapply to sapply

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is tapply() good for

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How is tapply used?

A

tapply(medical.example$age,medical.example$treatment, mean) … first two are groups to split at, third is the function wanted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain split()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly