I\'ve recently started using R and the apply()
function is tripping me up. I\'d appreciate help with this:
is.numeric(iris$Sepal.Length) # retur
They are all FALSE
because apply()
coerces iris
to a matrix before it applies the is.numeric()
function. From help(apply)
regarding the first argument, X
-
If
X
is not an array but an object of a class with a non-null dim value (such as a data frame),apply
attempts to coerce it to an array viaas.matrix
if it is two-dimensional (e.g., a data frame) or viaas.array
.
is.array(iris)
# [1] FALSE
So there you have it. The columns actually all become character after the coercion because a matrix can only take on one data type (see as.matrix(iris)
). The reason the whole thing is coerced to character and not some other data type is discussed in the Details section of help(as.matrix)
.
As Pascal has noted, you should use sapply()
.
sapply(iris, is.numeric)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# TRUE TRUE TRUE TRUE FALSE
Or the slightly more efficient vapply()
.
vapply(iris, is.numeric, NA)