Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does apply() return incorrect column types?

Tags:

r

apply

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) # returns TRUE
is.numeric(iris$Sepal.Width)  # returns TRUE
is.numeric(iris$Petal.Length) # returns TRUE
is.numeric(iris$Petal.Width)  # returns TRUE

but,

apply(iris, 2, FUN = is.numeric) 

returns

Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
       FALSE        FALSE        FALSE        FALSE        FALSE 

What's going on?

like image 347
Karan Tibrewal Avatar asked Feb 08 '23 05:02

Karan Tibrewal


1 Answers

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 via as.matrix if it is two-dimensional (e.g., a data frame) or via as.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)
like image 70
Rich Scriven Avatar answered Feb 16 '23 02:02

Rich Scriven