Yeah, my guess in the comment is right: you have factors!
sapply(Glass, class)
# RI Na Mg Al Si K Ca Ba
# "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric"
# Fe Type
# "numeric" "factor"
When you use apply()
, it will first coerce Glass
into a matrix. A matrix, like a vector, can only hold one type of data. Now, your data frame has both numeric and factor, the resulting matrix will be character only. skewness()
will have nothing to do in this case, as none of the columns is numeric (so you got NULL
).
If you use sapply()
or lapply()
, things are different. These functions are designed to work with lists / data frames. You will get valid result for all numeric columns.
Whether to use sapply()
or lapply()
depends on what you want. sapply()
returns a vector / matrix whenever it can, while lapply()
returns a list (by default). I reckoned that skewness()
only returns a scalar result, so recommended using sapply()
, by which you end up with a vector. If you want a data frame, use as.data.frame(lapply(Glass, skewness))
instead.