here, take a look at this
library(plyr)
# example data
dfx <- data.frame(
group = c(rep('A', 8), rep('B', 15), rep('C', 6)),
sex = sample(c("M", "F"), size = 29, replace = TRUE),
age = runif(n = 29, min = 18, max = 54)
)
#making one of the groups entirely NA on age
dfx$age <- ifelse(dfx$group=="C" & dfx$sex=='M',NA_real_,dfx$age)
dfx
# the problem
ddply(dfx, .(group, sex), summarize,
mean = round(mean(age), 2),
sd = round(sd(age), 2),
min = min(age,na.rm=TRUE))
#a solution with an explicit custom function
mymin <- function(x) {min(ifelse(is.na(x),0,x))}
ddply(dfx, .(group, sex), summarize,
mean = round(mean(age), 2),
sd = round(sd(age), 2),
mymin = mymin(age))
# could you make it fully anonymous? but then how to pass age to it ?
ddply(dfx, .(group, sex), summarize,
mean = round(mean(age), 2),
sd = round(sd(age), 2),
mymin = function(x) {min(ifelse(is.na(x),0,x))})
#how does it know we want to pass (age))