In this tutorial, you will learn about various data types in R programming language.
Data Types in R Programming –
To do programming in R, It’s important to understand how the values and data types works in R. Let’s start with literal values.
A literal value is part of a statement or expression. For instance, the numbers 170 and 5 are literal values because they literally represent the numbers
170 and 5. Every literal value has a type.
In R, there are five basic data types –
1 . Logical –
logical data type in R is also known as boolean data type. It can only have two values:
FALSE or simply
F. All the letters are in UPPERCASE, because R is case-sensitive it differentiates between uppercase and lowercase.
In programming languages, the logical literals represent two values:
1 (for true) and
0 (for false). Similarly, R also counts the
TRUE literal as
1 and the
FALSE literal as
For example, if we write
> bool1 <- TRUE > print(bool1)  TRUE > print(class(bool1))  "logical" > > bool2 <- FALSE > print(bool2)  FALSE > print(class(bool2))  "logical" >
The value of bool1 is TRUE and the value of bool2 is FALSE. And if we check the type of it, we can see that it says logical.
2. Numeric –
There are two types of numbers in R: integers (
Integer) and decimal/real (
170, 5, and 125 are integer numbers (i.e. whole numbers). 0.85, 3.14, and 0.0005 are decimal numbers. Note that the period symbol (
.) separates the integer from the decimal in decimal numbers.
For example, if we write
> num1 <- 0.05 > print(class(num1))  "numeric" >
The value of num1 is 0.05 and the data types is numeric.
3. Integer –
we learned that R considers any number (integer or decimal) a
Numeric literal. So, what if we want to represent a whole number as an
To force R to recognize numbers as integer literals (without the decimal part), the letter
L must accompany the number.
For example, we can write
> num2 <- 125L > print(num2)  125 > print(class(num2))  "integer"
4. Character –
We’ve established that R uses symbols, letters, or words (
FALSE) to accomplish tasks. However, sometimes we want to use words and characters as they are without any interpretation from R. We call these
We represent character literals by enclosing them in quotation marks (
") or apostrophes (
'). Quotation marks or apostrophes allow us to distinguish character values from other literal values (
Numeric). Here are some examples of character literals:
"Good Morning Folks ;-)".
Mixing quotation marks and apostrophes in the same character value is not valid syntax. For example, “Hello” won’t represent a character word if we have typed
Note that if we use “FALSE” (with quotation marks), R doesn’t consider it a logical literal; rather, it considers it a character literal. Similarly,
"12.4" isn’t a numeric literal but a character literal instead.
> word <- "Hello, world" > print(word)  "Hello, world" > print(class(word))  "character"
5. Complex –
complex data type is used to specify purely imaginary values in R. We use the suffix
i to specify the imaginary part.
> complex_val <- 5 + 3i > print(complex_val)  5+3i > print(class(complex_val))  "complex"