I have a data.frame "df" which has 200 observations and 18 columns. The 18 columns are var1, var2, etc.... When I use:
tapply(df$var1, INDEX=df$varX, FUN=mean, na.rm=T)
where varX is a fixed value of a certain variable (var) of type string, I get the mean of var1 for each value of varX. my question is: How may I put the above command in a for loop such that it would iterate the same command such that it will cover all variables (var1, var2, ...etc) except of course varX? I tried this:
for (k in c(var1, var2, ..., varn)) {
tapply(df$k, INDEX=df$varX, FUN=mean, na.rm=T)
}
But it did not work.
Please note: I am sure much more effective and elegant methods/scripts can be used, but since I am a beginner, and so much behind, I sometimes try to go ahead and apply some ideas before I finish reading the respective chapter of a book I have. This is why my method(s) sometimes look primitive.