I am having some difficulty counting non-missing values by group through the function below (which also gives sd, and mean):
test <- do.call(data.frame, aggregate(. ~ treatment, have, function(x) c(n = sum(!is.na(x)), mean = mean(x), sd = sd(x))))
It ends up giving me the number of non-missing for all columns in the dataframe instead of just a single column.
I have been looking through SO for some advice and found this, this, and this helpful, but I can't figure out why the aggregate with the function(x) would combine some columns for the sum(!is.na(x), but not for the mean or sd.
EDIT: Adding tables
This is the data I get from my code
You will notice in the 'have' dataframe that counting the non-mising rows in column var1 by treatment group gives the following:
veh - 9 gr.4 - 8 gr.3 - 10 gr.2 - 5
But when using the sum(!is.na(x) I get the following
veh - 6 gr.4 - 5 gr.3 - 10 gr.2 - 5
I believe this is because the function is using both var1 and var2 to sum the number of non-missing. I do not know how to correct for this.
Best,
Jack