apply() is giving NA values for every column
问题 I've been having this strange problem with apply lately. Consider the following example: set.seed(42) df <- data.frame(cars, foo = sample(LETTERS[1:5], size = nrow(cars), replace = TRUE)) head(df) speed dist foo 1 4 2 E 2 4 10 E 3 7 4 B 4 7 22 E 5 8 16 D 6 9 10 C I want to use apply to apply a function fun (say, mean ) on each column of that data.frame . If the data.frame is containing only numeric values, I do not have any problem: apply(cars, 2, mean) speed dist 15.40 42.98 But when trying