Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 525
sweetmusicality Avatar asked Nov 03 '17 07:11

sweetmusicality


People also ask

What is the difference between atomic vector and vector?

An atomic vector is different from a one-dimensional array: an array has a dim attribute of length one while a vector has no such attribute. An atomic vector is also different from a list. The elements of a vector are all of the same types while a list can contain any arbitrary type.

Is atomic () in R?

atomic() function in R is used to check if an R object is atomic or not. When an R object is atomic it can be used to create atomic vectors. Note: R has six basic atomic vector types: logical, integer, real, complex, string (or character), and raw.

Is atomic is not true?

atomic is true for the atomic types ( "logical" , "integer" , "numeric" , "complex" , "character" and "raw" ) and NULL . Most types of objects are regarded as recursive.

How do you check if something is an atomic vector in R?

Atomic data types are the object types that you can use to create atomic vectors. To check if any data object is atomic in R, use the is. atomic() function.


2 Answers

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.

like image 172
Roland Avatar answered Nov 14 '22 23:11

Roland


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.

like image 24
clemens Avatar answered Nov 14 '22 23:11

clemens