This may be the most inane question ever, but I'm having trouble understanding the function sapply: Here's the problem:
Example:
d stands for matrix (1:10, 5,2)
NA for d[3].
# [,1] [,2] [,3] [,4] [,5] [,6]
1 6 #[1,] #[1,] #[1,] #[1,] #
2 7 #[2,] #[2,] #[2,] #[2,] #
#[3,] NA #[3,] NA #[3,] NA #[3,]
##[4,] ##########################
#[5,] #[5,] #[5,] #[5,] #[5,
If I wanted to utilise the sapply function to calculate the row means, I'd do something like this:
sapply(d,mean)
#[1] NA 4 5 6 7 8 9 10 NA 4 5 6 7 8 9 10 NA 4 5 6 7 8 9 10 NA 4 5 6 7 8 9
Shouldn't it give me the average of the items' list? Instead of the mean, it just throws out the elements of my matrix.
I receive the correct response when I use apply:
apply(d,1,mean, na.rm=T, na.rm=T, na.rm=T, na.rm
[1] 3.5 4.5 8.0 6.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5
Is anyone willing to explain themselves to me?