R includes a variety of data types. These are numeric, integer, logical, complex and character data types in R. We go through these data types in detail. We will look at the differences among them.

For correct computational process, a programming language has to know what the thing at hand looks like. Therefore, R needs to know the type of data at hand.

R involves class() and typeof() functions to understand what is the class of any variable. R has five data types.

  1. Numeric
  2. Character
  3. Integer
  4. Logical
  5. Complex

Let’s go through these data types one-by-one. For practice, you can install R by following our steps for R installation.

Check Out: How to Download and Install R for Windows

1. Numeric in R

The numeric data type is used for numeric values. As an example, 2, 4, 7.1, -3, 9.2, -2,1, etc.

a <- 3 # numeric
class(a)
## [1] "numeric"

typeof(a)
## [1] "double"

The class of variable is ‘numeric’. When R stores a number in a variable, it converts the number into a ‘double’ value. We can convert a value into a numeric type using the as.numeric() function. The is.numeric() function returns a logical, TRUE or FALSE, to understand whether the data type is numeric or not.

Also Check: How to Change Working Directory in R

2. Character in R

The character data type is to store character values or strings. It is used with a character inside single or double inverted commas.

b <- "three" # character
class(b)
## [1] "character"

typeof(b)
## [1] "character"

We can convert a value into a character type using the as.character() function. The is.character() function returns a logical, TRUE or FALSE, to understand whether the data type is character or not.

3. Integer in R

The Integer data type is used to store integer values. We can define an integer in two ways. These are with ‘L’ and as.integer() function. Also, We can convert a value into a integer type using the as.integer() function.

c <- 3L # integer
c <- as.integer(3) # integer
class(c)
## [1] "integer"

typeof(c)
## [1] "integer"

The is.integer() function returns a logical, TRUE or FALSE, to understand whether the data type is integer or not.

Also Check: How to Install and Load a Package in R

4. Logical in R

The logical data type is to store a logical, TRUE or FALSE.

d <- TRUE # logical
class(d)
## [1] "logical"

typeof(d)
## [1] "logical"

The as.logical() function attempts to coerce its argument to be of logical type. The is.logical() function returns a logical, TRUE or FALSE, to understand whether the data type is logical or not.

5. Complex in R

The complex data type stores numbers with an imaginary component. As an example, 1-2i, 1+4i, 4-3i, etc.

e <- 1+2i # complex
class(e)
## [1] "complex"

typeof(e)
## [1] "complex"

The as.complex() function attempts to coerce its argument to be of complex type. The is.complex() function returns a logical, TRUE or FALSE, to understand whether the data type is complex or not.

Don’t forget to check: How to Import Data into R