Data types in R

Overview

Data types are an essential part of programming. The ability to identify the different data types in a programming language makes it easier for a programmer to code. The different data types mean that different data types can and can not do different things.

A variable helps store different data, which are of various types. We’ll look at the different data types in the R language in this shot.

Data types in R

There are five basic data types in R:

  1. character: Strings. For example, 'Theo', 'R', '10', and so on.
  2. integer: Positive or negative. For example, 10L, -20L, 100L, and so on. The L tells R to store the value as an integer.
  3. numerical: Numbers. For example, 1, 10, 1000, and so on.
  4. logical: Boolean (TRUE or FALSE)
  5. complex: Complex numbers. For example, 5 + 2i, 10 - 5i, and so on.
# character
name <- 'Theo'
# integer
x <- -10L
# numerical
y <- 200
# logical
a <- TRUE
# complex
z = 3 + 9i
name
x
y
a
z

Checking the data type

In the R language, we use the class() function to check the data type of a variable.

Code

# character
name <- 'Theo'
# integer
x <- -10L
# numerical
y <- 200
# logical
a <- TRUE
# complex
z = 3 + 9i
class(name)
class(x)
class(y)
class(a)
class(z)

Free Resources