# Data Types in R

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
 45
typeof(45L)
 "integer"
mode(45L)
 "numeric"
class(45L)
 "integer"
str(45L)
 int 45

### Numeric Data Type

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

45
 45
typeof(45)
 "double"
mode(45)
 "numeric"
class(45)
 "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
 TRUE
typeof(TRUE)
 "logical"
mode(TRUE)
 "logical"
class(TRUE)
 "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"
 "R Programming"
typeof("R Programming")
 "character"
mode("R Programming")
 "character"
class("R Programming")
 "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
 2+3i
typeof(2+3i)
 "complex"
mode(2+3i)
 "complex"
class(2+3i)
 "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)
 FALSE
# check whether 45 is integer type data object
is.integer(45L)
 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)
 TRUE
# check whether 45 is double type data object
is.double(45L)
 FALSE
# check whether Inf is double type data object
is.double(Inf)
 TRUE
# check whether Inf is double type data  object
is.double(NaN)
 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)
 TRUE
# check whether 45L is numeric type data object
is.numeric(45L)
 TRUE
# check whether 0x11 is numeric type data object
is.numeric(0x11)
 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)
 TRUE
# check whether FALSE is logical type data object
is.logical(FALSE)
 TRUE
# check whether F is logical type data object
is.logical(F)
 TRUE
# check whether NA is logical type data object
is.logical(NA)
 TRUE
# check whether 'FALSE' is logical type data object
is.logical('TRUE')
 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')
 TRUE
# check whether "Male" is character type data object
is.character("Male")
 TRUE
# check whether TRUE is character type data object
is.character(TRUE)
 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)
 TRUE
# check whether 1i is character type data object
is.complex(1i)
 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.