Contents

In this tutorial, we will discuss about various data types in R, some functions related to data types and functions to check whether object belongs to some specific data type.

## Data Types in R

Data types are the objects using which we can create vectors in R. All the objects stored in R have a class. The class tells R how to handle the object. The most common data types in R are five basic classes of an objects.

- Integer
- Numeric (Real numbers)
- Logical (True/False)
- Character
- Complex

## Some useful functions for Data Types in R

R provides some functions to get the data types of an R object. The functions are as follows:

`typeof()`

: Determines the type or storage mode of object.`mode()`

: Get or set the type or storage mode of an object.`storage.mode()`

: Get or set the type or storage mode of an object.`class()`

: Test the class of R object.`str()`

: Compactly display the structure of R object.

Following table shows the different data type along with their mode, storage mode in R and examples:

Data Type | Type of | Mode | Storage | Examples |
---|---|---|---|---|

Integer | integer | numeric | integer | 4L, -5L, 2e03L |

Numeric | double | numeric | double | 1, -2.5, 2e-03 |

Logical | logical | logical | logical | TRUE, FALSE, T, F |

Character | character | character | character | "Hello" |

Complex | complex | complex | complex | 3 + 2i, 1i |

### Integer Data Type

Integer data type can be created with whole number followed by L.

`45L # integer data type`

`[1] 45`

`typeof(45L)`

`[1] "integer"`

`mode(45L)`

`[1] "numeric"`

`class(45L)`

`[1] "integer"`

`str(45L)`

` int 45`

### Numeric Data Type

The numeric data type consists of double (real) and integer data types.

`45`

`[1] 45`

`typeof(45)`

`[1] "double"`

`mode(45)`

`[1] "numeric"`

`class(45)`

`[1] "numeric"`

`str(45)`

` num 45`

Note that `Inf`

, `-Inf`

, `NaN`

and hexadecimal numbers are of numeric (double) data types.

### Logical Data Type

Logical data type contains `TRUE`

, `FALSE`

and `NA`

. Single character can also be used for logical data type i.e., `T`

for `TRUE`

and `F`

for `FALSE`

(without quotes).

`TRUE`

`[1] TRUE`

`typeof(TRUE)`

`[1] "logical"`

`mode(TRUE)`

`[1] "logical"`

`class(TRUE)`

`[1] "logical"`

`str(TRUE)`

` logi TRUE`

### Character Data Type

Character data type can be created by enclosing sequence of strings using single quote `'`

or double quote `"`

.

`"R Programming"`

`[1] "R Programming"`

`typeof("R Programming")`

`[1] "character"`

`mode("R Programming")`

`[1] "character"`

`class("R Programming")`

`[1] "character"`

`str("R Programming")`

` chr "R Programming"`

### Complex Data Type

Complex data type contains complex numbers. A numeric value followed by i. Only pure imaginary numbers are complex constants. e.g. 1i, 0i, 2.3e-1i are valid complex constants.

`2+3i`

`[1] 2+3i`

`typeof(2+3i)`

`[1] "complex"`

`mode(2+3i)`

`[1] "complex"`

`class(2+3i)`

`[1] "complex"`

`str(2+3i)`

` cplx 2+3i`

## Check data type of data object in R

R provides some functions to check if the data object is of specific type.

### is.integer() function in R

Use function `is.integer()`

to check whether the data is of integer type. The function return `TRUE`

is the data object is integer otherwise return `FALSE`

.

```
# check whether 45 is integer type data object
is.integer(45)
```

`[1] FALSE`

```
# check whether 45 is integer type data object
is.integer(45L)
```

`[1] TRUE`

### is.double() function in R

Use function `is.double()`

to check whether the data is of double or real type. The function return `TRUE`

is the data object is double otherwise return `FALSE`

.

```
# check whether 45 is double type data object
is.double(45)
```

`[1] TRUE`

```
# check whether 45 is double type data object
is.double(45L)
```

`[1] FALSE`

```
# check whether Inf is double type data object
is.double(Inf)
```

`[1] TRUE`

```
# check whether Inf is double type data object
is.double(NaN)
```

`[1] TRUE`

### is.numeric() function in R

Use function `is.numeric()`

to check whether the data is of numeric type. The function return `TRUE`

is the data object is numeric otherwise return `FALSE`

.

```
# check whether 45 is numeric type data object
is.numeric(45)
```

`[1] TRUE`

```
# check whether 45L is numeric type data object
is.numeric(45L)
```

`[1] TRUE`

```
# check whether 0x11 is numeric type data object
is.numeric(0x11)
```

`[1] TRUE`

`0x11`

is the hexadecimal representation of 17.

Note that numeric data type in R is composed of integer as well as real (double) data types.

### is.logical() function in R

Use function `is.logical()`

to check whether the data is of logical type. The function return `TRUE`

is the data object is logical otherwise return `FALSE`

.

```
# check whether TRUE is logical type data object
is.logical(TRUE)
```

`[1] TRUE`

```
# check whether FALSE is logical type data object
is.logical(FALSE)
```

`[1] TRUE`

```
# check whether F is logical type data object
is.logical(F)
```

`[1] TRUE`

```
# check whether NA is logical type data object
is.logical(NA)
```

`[1] TRUE`

```
# check whether 'FALSE' is logical type data object
is.logical('TRUE')
```

`[1] FALSE`

Note that logical data type contains `TRUE`

, `FALSE`

, `T`

, `F`

or `NA`

(without quote).

### is.character() function in R

Use function `is.character()`

to check whether the data is of character type. The function return `TRUE`

is the data object is character otherwise return `FALSE`

.

```
# check whether 'Male' is character type data object
is.character('Male')
```

`[1] TRUE`

```
# check whether "Male" is character type data object
is.character("Male")
```

`[1] TRUE`

```
# check whether TRUE is character type data object
is.character(TRUE)
```

`[1] FALSE`

As `TRUE`

is not enclosed within single or double quote, it is not considered as a character.

Note that character data type in R is composed of sequence of string or characters using single or double quote.

### is.complex() function in R

Use function `is.complex()`

to check whether the data is of complex type. The function return `TRUE`

is the data object is complex otherwise return `FALSE`

.

```
# check whether 2+3i is complex type data object
is.complex(2+3i)
```

`[1] TRUE`

```
# check whether 1i is character type data object
is.complex(1i)
```

`[1] TRUE`

## Endnote

In this tutorial you learned about different data types in R and how to check their class, mode and storage mode in R. Also how to check the data type of data object in R.

Learn more about data structures in R refer to the following tutorials:

- Variables and constants in R
- Data Structures in R
- Vectors in R
- Matrix in R
- Arrays in R
- Lists in R
- Factors in R
- Data Frames in R

Hopefully you enjoyed learning this tutorial on data types in R. Hope the content is more than sufficient to understand data types in R.