In a previous link: Calculating a group mean while excluding each cases individual value
matt_k made a clever answer to compute group means excluding the individual. He propose the following:
set.seed(123)
df <- data.frame(group = rep(letters[1:3], each = 3),
value = rnorm(9), stringsAsFactors = F)
df$loo_mean <- unlist(tapply(df$value, df$group,
function(x) (sum(x) - x) / (length(x) - 1)))
df
But the code does not deal NA's properly, as it yield NA's for all the individuals of the group if there is a NA for the group. Can anyone solve the problem?