Search code examples
rvectordata-structuresatomictypeof

is.atomic() vs is.vector()


I don't understand the difference between is.atomic() and is.vector(). From my understanding, is.vector() returns TRUE for homogeneous 1D data structures. I believe is.atomic() returns TRUE for logicals, doubles, integers, characters, complexes, and raws...however, wouldn't is.vector() as well? So I thought perhaps the difference lies in its dimensions, but is.atomic() returned FALSE on a dataframe of doubles, which made me even more confused, ah...

Also, what is the difference between an atomic vector and a normal vector?

Thanks for your clarification!


Solution

  • Atomic vectors are a subset of vectors in R. In the general sense, a "vector" can be an atomic vector, a list or an expression. The language definition sort of defines vectors as "contiguous cells containing data". Also refer to help("is.vector") and help("is.atomic"), which explain when these return TRUE or FALSE.

    is.vector(list())
    #[1] TRUE
    is.vector(expression())
    #[1] TRUE
    is.vector(numeric())
    #[1] TRUE
    
    is.atomic(list())
    #[1] FALSE
    is.atomic(expression())
    #[1] FALSE
    is.atomic(numeric())
    #[1] TRUE
    

    Colloquially, we usually mean atomic vectors (possibly even with attributes) when we talk about vectors.