I\'ve been having this strange problem with apply lately. Consider the following example:
set.seed(42)
df <- data.frame(cars, foo = sample(LE
apply works on a matrix, and a matrix must be of all one type. So df is being transformed into a matrix, and since it contains a character, all the columns are becoming character.
> apply(df, 2, class)
speed dist foo
"character" "character" "character"
To get what you want, check out the colwise and numcolwise functions in plyr.
> numcolwise(mean)(df)
speed dist
1 15.4 42.98