# Atomic Vector in R Tutorial with Example

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.

## Atomic Vector in R

Atomic vector is a fundamental data structure in the R programming language. An atomic vector is different from a one-dimensional array because it has a dim attribute of length while a vector has no such attribute. Atomic vector is also different from the list since the vector items have the same types, while a list can contain various arbitrary data types.

## Types of Atomic Vector in R

There are five types of atomic vectors.

1. Numeric: integer and double (real)
2. Character
3. Logical
4. Complex
5. Raw

## Integer vectors

The integer data type is a type of numeric data type. To create an integer vector in R, use the integer() function.

``integer(length = 7)``

#### Output

`` 0 0 0 0 0 0 0``

### as.integer()

The as.integer() is a built-in R function that attempts to coerce its argument to be of integer type.

``````rv <- "10"
ind <- as.integer(rv)
ind
typeof(ind)``````

#### Output

`````` 10
 "integer"``````

### is.integer()

To check if the argument is an integer or not, use the is.integer() function. The is.integer() method returns TRUE or FALSE depending on if the input is an integer or not.

``````rv <- "10"
ind <- as.integer(rv)
ind
is.integer(ind)``````

#### Output

`````` 10
 TRUE``````

## Character Vectors

Character vectors consist of characters. To create a character vector in R, use the character() method. Text in R is represented by character vectors. You can also assign a character value to a Vector sing the assignment operator (<- ), and it becomes the character vector.

To create an empty character vector, use the character() function and pass the length of that empty vector.

``````rc <- character(5)
rc``````

#### Output

`` "" "" "" "" ""``

### as.character()

The as.character() is a built-in R method that tries to coerce its argument to character type; like as.vector() method, it strips attributes including names.

``````rc <- 10
rnd <- as.character(rc)
rnd
typeof(rnd)``````

#### Output

`````` "10"
 "character"``````

As you can see that we defined an integer vector, and then using as.vector() function, we converted integer vector to character vector. To check the data type of a variable in R, use the typeof() function.

### is.character()

To check if the argument is a character or not, use the is.character() function. The is.character() method returns TRUE or FALSE depending on if the input is a character vector or not.

``````rc <- 10
rnd <- as.character(rc)
rnd
is.character(rnd)``````

#### Output

`````` "10"
 TRUE``````

## Logical Vectors

Logical vectors are the vectors that only contain logical values like TRUE or FALSE. To create a logical vector in R, use the logical() function.

``````rl <- logical(length = 4)
rl``````

#### Output

`` FALSE FALSE FALSE FALSE``

### as.logical()

The as.logical() is a built-in R method that tries to coerce its argument to logical type.

``````rl <- 1
lnd <- as.logical(rl)
lnd
typeof(lnd)``````

#### Output

`````` TRUE
 "logical"``````

As you can see that we defined an integer vector, and then using as.logical() function, we converted integer vector to logical vector.

### is.logical()

To check if the argument is a logical vector or not, use the is.logical() function. The is.logical() method returns TRUE or FALSE depending on if the input is a logical vector or not.

``````rl <- 1
lnd <- as.logical(rl)
lnd
is.logical(lnd)``````

#### Output

`````` TRUE
 TRUE``````

## Complex vectors

The complex vectors can be created with complex() function. Pass the length you want to create, and it will create a complex vector for you.

``````rcom <- complex(length = 4)
rcom``````

#### Output

`` 0+0i 0+0i 0+0i 0+0i``

### as.complex()

The as.complex() is a built-in function that tries to coerce its argument to be of complex type: like as.vector(), it strips attributes including names.

``````rcom <- 1
cnd <- as.complex(rcom)
cnd
typeof(cnd)``````

#### Output

`````` 1+0i
 "complex"``````

As you can see that we defined an integer vector, and then using the as.complex() function, we converted integer vector to complex vector.

### is.complex()

To check if the argument is a complex vector or not, use the is.complex() function.

``````rcom <- 1
cnd <- as.complex(rcom)
cnd
is.complex(cnd)``````

#### Output

`````` 1+0i
 TRUE``````

## Raw Vectors

Raw vectors are used to store fixed-length sequences of bytes. You can create a raw vector of the specified length. Each element of the vector is equal to 0. To create a raw vector in R, use the raw() function.

``````rawv <- raw(length = 4)
rawv``````

#### Output

`` 00 00 00 00``

### as.raw()

The as.raw() is a built-in function that tries to coerce its argument to be raw.

``````rw <- 1
rawd <- as.raw(rw)
rawd
typeof(rawd)``````

#### Output

`````` 01
 "raw"``````

### is.raw()

To check if the argument is a raw vector or not, use the is.raw() function.

``````rw <- 1
rawd <- as.raw(rw)
rawd
is.raw(rawd)``````

#### Output

`````` 01
 TRUE``````

As you can see that the is.raw() method returns TRUE since we have passed a raw vector to the is.raw() function.

That is it for atomic vectors in R.

Categories R