8

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!

Sotos
  • 51,121
  • 6
  • 32
  • 66
sweetmusicality
  • 937
  • 1
  • 10
  • 27

2 Answers2

9

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.

Roland
  • 127,288
  • 10
  • 191
  • 288
1

Vectors in R can have 2 structures, the first one, atomic vectors, and the second one, lists.

If you create a new, empty vector, you can specify the mode to get an empty list vector(mode = "list") which returns the same as list().

identical(vector(mode = "list"), list())
[1] TRUE

is.vector(vector(mode = "list")) returns [1] TRUE, whereas is.atomic(vector(mode = "list")) returns [1] FALSE.

clemens
  • 6,653
  • 2
  • 19
  • 31