R Data Types – Master Their Usage and Implementation!

post

We’ll explore in detail the different types of data in R, including numeric, integer, logical, complex, and character types.

R Data Types – Master Their Usage and Implementation!

Continuing with our R Programming Tutorial Series, today we dive into R data types. We’ll explore in detail the different types of data in R, including numeric, integer, logical, complex, and character types. We’ll also learn how they differ from each other and how to create variables for each type.

Let’s get started!

What are Data Types in R?

In any programming language, it’s crucial to define what kind of operations are allowed on different types of values.
For instance, you can't perform addition on words like "hello" and "world", or change numbers like 1 or -34.5 into uppercase or lowercase letters.

To manage such distinctions, R assigns data types to different kinds of values. Each data type comes with specific characteristics and rules that define how it behaves.

Choosing the right data type is also important for better memory usage and faster code performance.
In R, using appropriate data types makes your code more efficient, readable, and maintainable.

You can check the type and class of any variable using the functions class() and typeof().

R mainly uses five data types:

Numeric

Integer

Complex

Logical

Character

Let’s explore each one in detail.

Tip: If you haven’t already installed R and RStudio, check out a beginner-friendly guide to get set up.

1. Numeric Data Type

Numeric data types represent real numbers (both integers and decimals).
In R, numeric is the default type for numbers.

Examples:
1, 34.5, 3.145, -24, -45.003, etc.

Example Code:

num <- 1 class(num) typeof(num) 

Note:
R internally stores numbers as double precision (floating-point) by default.
Even if you write 1, R actually stores it as 1.00 (class = numeric, type = double).

2. Integer Data Type

Integer type is used for whole numbers without any decimal point.

To explicitly define a number as an integer, you can either:

Use the as.integer() function, or

Append an uppercase L after the number.

Example Code:

int <- as.integer(16) class(int) typeof(int) int2 <- 5L class(int2) typeof(int2) 

3. Complex Data Type

Complex numbers have both a real and an imaginary part.

Examples:
1+2i, 3i, 4-5i, -12+6i

Example Code:

comp <- 22-6i class(comp) typeof(comp) 

4. Logical Data Type

Logical values are either TRUE or FALSE.
They are mainly used in conditions, comparisons, and decision-making.

Example Code:

logi <- FALSE class(logi) typeof(logi) 

5. Character Data Type

Character data types store text strings, including letters, numbers, and symbols.
In R, you enclose character values within single or double quotes.

Example Code:

char <- "learnR" class(char) typeof(char) 

You can also use the as.character() function to convert values into characters:

char2 <- as.character(1234) class(char2) typeof(char2) 

Converting Data Types in R

In R, you can explicitly convert between data types using specific functions:

as.numeric()

as.integer()

as.complex()

as.logical()

as.character()

Let’s see how conversions work!

Conversion into Numeric

Use as.numeric() to convert other data types into numeric.

Integers become numeric with decimals.

Complex numbers lose the imaginary part.

Logical TRUE → 1 and FALSE → 0.

Characters that are pure numbers (like "123") can be converted. Others result in NA.

Example Code:

as.numeric(as.integer(10))     # 10 as.numeric(3+2i)               # 3 as.numeric(TRUE)               # 1 as.numeric("1234")             # 1234 as.numeric("abc")              # NA 

Conversion into Integer

Use as.integer() to convert values into integers.

Numeric decimals are truncated (not rounded).

Complex values lose their imaginary parts.

Logical TRUE → 1 and FALSE → 0.

Characters with numeric content can be converted.

Example Code:

as.integer(14.7)     # 14 as.integer(5+3i)     # 5 as.integer(TRUE)     # 1 as.integer("123")    # 123 

Conversion into Complex

Use as.complex() to convert other types into complex numbers.

Adds an imaginary part 0i if missing.

Logical values become 1+0i or 0+0i.

Example Code:

as.complex(5)        # 5+0i as.complex(TRUE)     # 1+0i as.complex("123")    # 123+0i 

Conversion into Logical

Use as.logical() to convert other types into logical values.

Nonzero numbers → TRUE, 0 → FALSE

Characters return NA

Example Code:

as.logical(0)         # FALSE as.logical(5)         # TRUE as.logical("hello")   # NA 

Conversion into Character

Use as.character() to convert any data type into a string.

Example Code:

as.character(123)     # "123" as.character(TRUE)    # "TRUE" as.character(5+3i)    # "5+3i" 

Note:
NA values stay NA during any type conversion.

Summary

In this tutorial, we explored the five core data types in R.
They are the foundation for data storage and manipulation in R programming.
Choosing the right data type and understanding how to convert between them is crucial for writing efficient and reliable R code.


Share This Job:

Write A Comment

    No Comments