I have this dataframe:
set.seed(50)
data <- data.frame(age=c(rep(\"juv\", 10), rep(\"ad\", 10)),
sex=c(rep(\"m\", 10), rep(\"f\", 10)),
Use aggregate:
nacheck <- function(var, factor)
aggregate(var, list(factor), function(x) sum(is.na(x)))
nacheck(data$length, data$age)
nacheck(data$length, data$sex)
nacheck(data$length, data$size)
You could also apply this to your dataframe, by each factor to get NA counts for all of the dimension measures for each factor.
apply(data[,c("length","width","height")], 2, nacheck, factor=data$age)
apply(data[,c("length","width","height")], 2, nacheck, factor=data$sex)
apply(data[,c("length","width","height")], 2, nacheck, factor=data$size)
To do this all as one function, nest nacheck in something and then lapply:
exploreNA <- function(df, factors){
nacheck <- function(var, factor)
aggregate(var, list(factor), function(x) sum(is.na(x)))
lapply(factors, function(x) apply(df, 2, nacheck, factor=x))
}
exploreNA(data[,c("length","width","height")], list(data$age, data$sex, data$size))