Reputation: 393
This might be the most stupid question, but I do not seem to grasp the function sapply: Here is my issue
Example:
d = matrix(1:10, 5,2)
d[3] = NA
# [,1] [,2]
#[1,] 1 6
#[2,] 2 7
#[3,] NA 8
#[4,] 4 9
#[5,] 5 10
If I would like to calculate the row means using sapply function I would use something like this:
sapply(d,mean)
#[1] 1 2 NA 4 5 6 7 8 9 10
Should it not give me the mean of the list of the elements? It just spits out the elements of my matrix rather then the mean.
When I use apply, I get the right answer:
apply(d,1,mean, na.rm=T)
[1] 3.5 4.5 8.0 6.5 7.5
Can anyone bother giving me a very dummy explanation.Highly appreicated. Used the following links before asking the question. link 1 Link 2 Link 3
Upvotes: 0
Views: 16153
Reputation: 926
sapply
(and its friends, like lapply
) require a list (or a data.frame, which is really a special kind of list) as input. But even if you had turned your matrix into a data frame, it wouldn't have given you row means, it would have given you column means. If you want to understand how these functions work, it might help to look at this function (copied from here: http://adv-r.had.co.nz/Functionals.html), which shows the essence of lapply
using base R. (sapply
works in the same way, it just tries to simplify the output to a vector rather than always returning a list.)
lapply2 <- function(x, f, ...) {
out <- vector("list", length(x))
for (i in seq_along(x)) {
out[[i]] <- f(x[[i]], ...)
}
out
}
By the way, if the point of your question is to find the best way of calculating row means, rather than understanding sapply
, then there is a function rowMeans
that is the fastest way of doing it -- faster than using apply
, for example.
Upvotes: 2
Reputation: 18580
have you read ?sapply
. The function takes either a vector or an expression object. It cannot take a matrix. So what happens in your example is that the matrix d
is considered as a vector of numeric:
sapply(as.numeric(d),mean)
Upvotes: 2