Q:

How can I incorporate NA removal into aggregate based on a custom function?

This is my first time using any custom functions, so bear with me. I made a function for standard error that I'd like to use with aggregate. It worked until I tried to exclude NAs. 
Dummy data frame to work with:
se <- function(x) sd(x)/sqrt(length(x))
df <- data.frame(site = c('N','N','N','S','S','S'),
                birds = c(NA,4,2,9,3,1), 
                worms = c(2,1,2,4,0,5))
means <- aggregate(df[,2:3], na.rm = T, list(site = df$site), FUN = mean)
error <- aggregate(df[,2:3], na.rm = T, list(site = df$site), FUN = se)

So aggregate worked before I excluded NAs (e.g. error <- aggregate(df[,2:3], list(site = df$site), FUN = se)), and it works when finding the mean (using the rest of the values to take the mean and ignoring the missing value). How can I exclude NAs in that same manner when using my custom se function?

A:

The problem is that you do not have an explicit argument for na.rm in your se function. If you add that to your function, it should work: 
se <- function(x, na.rm = TRUE) {
    sd(x, na.rm = na.rm)/sqrt(sum(!is.na(x)))
}

