# data types in R: Introduce the basic data types in R

In this article, we will share with you the definition and how to use the data types in R. R has five basic data types, which we usually use: Numeric, Integer, Character, Complex, and Logical. Follow us to learn more about it with the explanation and examples below.

## Data Types In R

### Which are data types in R?

When working with R or other languages, data types are the basic and the most important knowledge when you begin learning programming languages. There are five basic data types in R: Numeric, Integer, Character, Complex, and Logical. The data types usually combine with the data structures like vector, data frame,… And it is used with almost all statements in your code. So, it is basic but essential.

Let’s follow us to learn more about the definition and how to use each data type in R in the next title below.

## The Definition And How To Use The Data Types In R

### Numeric

The numeric in R is a number. In other words, it is an integer or a decimal.

Look at the example below.

# The numeric vector without decimals.
number <- c(1, 4, 6, 7, 9)
class(number)

Output

[1] "numeric"
# The numeric decimals.
number <- c(1.1, 4.2, 6.2, 7.3, 9.5)
class(number)

Output

[1] "numeric"
# Combine the numeric decimals and the integer.
number <- c(1.1, 4.2, 6.2, 7.3, 9.5)
class(number)

Output

[1] "numeric"

### Integer

In the previous title, we mentioned the numeric in R. And the integer in R is also numeric in R without decimals.

But you can not create the integer types normally, you can use the as.integer() function to create the integer types.

Syntax

as.integer(data)

Parameters

• data: The object you want to convert to the integer type.

Look at the example below.

# Create the integer type.
vec <- as.integer(c(1, 2, 3, 4))
class(vec)

Output

[1] "integer"

### Complex

The complex data type is the complex number which has two parts: the real part and the imaginary part. The real part is the numeric, and ‘i’ represents the imaginary part.

Look at the example below.

# Create the complex data type.
complex_type <- 12+ 2i
class(complex_type)

Output

[1] "complex"

Note that you have to assign the factor to the ‘i’ part to prevent getting the error.

### Character

In R, the character data type is used to store the String value. You can create a value as a character type by assigning the value to the single quotation or the double quotation marks. You can use the as.character() function to convert the object to the character type. This function is the same syntax as the as.integer() function I have mentioned above.

Look at the example below.

# Create the character type.
chr <- 'LearnShareIT'
class(chr)

Output

[1] "character"

Use the as.character() function to convert the object to the character type.

# Convert the numeric to a character.
chr <- as.character(1)
chr # [1] "1"
class (chr)

Output

[1] "character"

### Logical

The logical type in R is used to check whether a variable, or events in R is true or false. The logical type has two values: TRUE or FALSE. Like other data types, R also has the as.logical() function to convert the object to the logical type.

Look at the example below.

# Create the logical type.
var <- TRUE
class(var)

Output

[1] "logical"

To convert an object to a logical type.

# Convert the numeric to logical type.
var <- as.logical(1)
var # [1] TRUE
class(var)

Output

[1] "logical"

## Summary

We have introduced you to the definition, and how to use the data types in R. You can also convert between data types by the as.name_data_type() function. We hope this tutorial is helpful to you. Thanks!

Posted in R