Why does apply() return incorrect column types?

后端 未结 1 1173
栀梦
栀梦 2021-01-19 16:00

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         


        
相关标签:
1条回答
  • 2021-01-19 16:25

    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)
    
    0 讨论(0)
提交回复
热议问题