# R Data Types – Elementary variables used in R calculations

Finally, the time has come to get our hands dirty and explore the basics of the R programming language.

Variables are the key building blocks in any programming language. As you may have noticed, these variables always have a type. This type shows what kind of data stored in the variable.

Every language has certain data types it can handle. In this tutorial, we will learn about the various R data types. Let’s jump right into it!

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

## What are the Data Types in R?

There are fundamentally five data types in R. Though straight forward and obvious at first glance, they have a few surprises hidden in them. These elementary data types are:

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

These data types are often combined to form data structures. Let us explore the meaning of each data type in detail.

But before reading further it is recommended to install R & RStudio on your system by following our step by step article for R installation.

### 1. Numeric Data Type

Numeric data consists of decimal values.

> num <- 12.5 #assigns a value of 12.5 to the variable num
> num #shows the value of the variable num

Output:

 12.5 The value doesn’t have to be decimal for the variable to be numeric. For example, the following code will result in a numeric value as well.

> num2 <- 5 #assigns a value of 5 to the variable num2
> num2
> class(num2) #shows the class or type of the variable num2

Output:

 5
 “numeric” The numeric data type works by default. If you assign a number to any variable it has a type of numeric unless specified otherwise.

Note: the numeric class is a collection of multiple classes. The most common of these are “double (for double-precision floating-point numbers)” and “integers”.

### 2. Integer data type

In R, there are two ways to create an integer variable. The first is to invoke the as.integer() function.

> int <- as.integer(3) #makes int an integer with value 3
> int
> class(int)

Output:

 3
 “integer” Another method of creating an integer variable is to use ‘the capital L’.

> int2 <- 5L #makes int2 an integer with value 5
> int2
> class(int2)

Output:

 5
 “integer” Note: In R, numeric is the default type for numbers. It stores all numbers as floating-point numbers (numbers with decimals). This is because most statistical calculations deal with numbers with up to two decimals.

It is easier to store numbers with decimals than to convert integers into numeric whenever they are needed for calculations.

R handles the conversion behind-the-scenes by storing them all as numerics. We recommend using the integer data type only when you know that you will not have to convert the variable. For eg: ID values or indexing.

### 3. Complex data type

The complex data type in R is for complex numbers or numbers with imaginary values.

> comp <- 12 + 3i #makes comp a complex type with value 12+3i
> comp
> class(comp)

Output:

 12+3i
 “complex” Note: In R, ‘i’ signifies an imaginary number only when suffixed to a number. ‘i’ on its own can be a variable of any kind.

For eg: 5i is an imaginary number while ‘i’ alone can be a variable.

### 4. Character data type

The character data type is used to store strings in R. A character variable can be created in two ways in R.

The first is to invoke the as.character() function.

> char <- as.character("techvidvan") #makes a character variable char with value "techvidvan"
> char
> class(char)

Output:

 “techvidvan”
 “character” Another way to create a character variable is by using inverted commas.

> char2 <- "something" #makes a character variable char with value "something"
> char2
> class(char2)

Output:

 “something”
 “character” Note: Within the inverted commas, everything is a character. Thus, a variable assigned a value of “3.14” will be of character type.

### 5. Logical data type

A logical variable can have two values either TRUE or FALSE. Logical are generally created when there is a comparison between variables.

> a <- 5
> b <- 6
> log <- a < b #assigns TRUE to k if a < b else FALSE
> log
> class(log)

Output:

 TRUE
 “logical”

Note: The as.numeric() function can convert logical variables into numerics. They take the value of 0 for FALSE and 1 for TRUE. Conversely, the as.logical() function can convert numeric values into logical, giving the value FALSE for 0 and TRUE for anything else.

## Data structures in R

R operates with named data structures. These data structures store data in an organized manner. This makes data manipulation and other data operations more efficient. There are many data structures in R like vectors, matrices, data frames, lists, etc. These data structures contain elements that may be values of one of the basic data types or other data structures.

## Summary

In this tutorial, we explored the elementary data types in R. These types are numerics, integers, complex, logical, and characters. We also learned about their weird quirks, the differences between them and about what are their uses.

If you are a newbie to R programming don’t skip many more R features and take full advantage of R programming.

And if you like the R Data Types article, do rate us on Google and share your feedback in the comment section.

Keep learning!