Site icon DataFlair

R Data Types – Become an expert in its implementation!

R data types

FREE Online Courses: Dive into Knowledge for Free. Learn More!

Moving ahead in our R DataFlair Tutorial Series, today we will learn all about R data types in which we will understand about numeric, integer, logical, complex, and character values in R programming in detail. We will also look at the differences amongst them and how to create variables containing these types of values.

So, let’s start with the tutorial.

What are Data types in R?

For correct processing, a programming language must know what can and cannot be done to a particular value. For example, addition cannot be performed on the words ‘hello’ and ‘world’.

Similarly, you cannot change the numbers 1 and -34.5 from lower to uppercase. Due to this, R has a feature called the data types. Different kinds of values are assigned different data types that help differentiate them. These types have certain characteristics and rules associated with them that define their properties.

R provides the class() and typeof() functions to find out what is the class and type of any variable. R has five data types which are:

  1. Numeric
  2. Integers
  3. Complex
  4. Logical
  5. Characters

Let’s go through these data types one-by-one.

For practice, you can install R & RStudio by following our step by step R installation tutorial.

1. Numeric Data Type

The numeric data type is for numeric values. It is the default data type for numbers in R.
Examples of numeric values would be 1, 34.5, 3.145, -24, -45.003, etc.

Code:

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

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Output:

Note: When R stores a number in a variable, it converts the number into a ‘double’ value or a decimal type with at least two decimal places. This means that a value such as ’1’ is stored as 1.00 with a type of double and a class of numeric.

You can check the Numeric and Character Functions in R for better understanding.

2. Integers Data Type

The Integer data type is used for integer values. To store a value as an integer, we need to specify it as such. The integer data type is commonly used for discrete only values like unique ids. We can store as well as convert a value into an integer type using the as.integer() function.

Code:

> int <- as.integer(16)
> class(int)
> typeof(int)
> int2 <- as.integer(num)
> int2
> class(int2)
> typeof(int2)

Output:

We can also use the capital ‘L’ notation to denote that a particular value is of the integer data type.

Code:

> int3 <- 5L
> class(int3)
> typeof(int3)

Output:

3. Complex Data Type

The complex data type is to store numbers with an imaginary component. Examples of complex values would be 1+2i, 3i, 4-5i, -12+6i, etc.

Code:

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

Output:

4. Logical Data Type

The logical data type stores logical or boolean values of TRUE or FALSE.

Code:

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

Output:

5. Character Data Type

The character data type stores character values or strings. Strings in R can contain the alphabet, numbers, and symbols. The easiest way to denote that a value is of character type in R is to wrap the value inside single or double inverted commas.

Code:

> char <- "dataflair1234"
> class(char)
> typeof(char)

Output:

We can also use the as.character() function to store a value as a character or to convert a value to the character data type.

Code:

> char2 <- as.character("hello")
> char3 <- as.character(comp)
> char2
> char3
> class(char2)
> typeof(char2)
> class(char3)
> typeof(char3)

Output:

Converting Data Types in R

We can convert values from one data type to another (if possible). R has certain rules that govern these conversions.

Conversion into Numeric

We can use the as.numerical function to convert the values of other data types into numerical values. The conversion follows a few rules, which are:

Code:

> num2 <- as.numeric(int)
> num2
> num3 <- as.numeric(comp)
> num3
> num4 <- as.numeric(logi)
> num4
> num5 <- as.numeric(char)
> num5 <- as.numeric("1234")
> num5

Output:

Conversion into Integer

The as.integer function can convert the values of other data types into integer values according to the following rules:

Code:

> int4 <- as.integer(num)
> int4
> int5 <- as.integer(14.7)
> int5
> int6 <- as.integer(comp)
> int6
> int7 <- as.integer(logi)
> int7
> int8 <- as.integer("1234")
> int8

Output:

Conversion into Complex

Using the as.complex function, we can convert other values into the complex data types. The conversion takes place according to the following rules:

Code:

> comp2 <- as.complex(num)
> comp2
> comp3 <- as.complex(int)
> comp3
> comp4 <- as.complex(logi)
> comp4
> comp5 <- as.complex("1234")
> comp5

Output:

Conversion into Logical

Conversion into logical data type can be done by using the as.logical function, by following the given rules:

Code:

> logi2 <- as.logical(num)
> logi2
> logi3 <- as.logical(int)
> logi3
> logi4 <- as.logical(comp)
> logi4
> logi5 <- as.logical(char)
> logi5

Output:

Conversion into character

We can convert a value of any data type into character data type using the as.character function. The function converts the original value into a character string.

Code:

> char2 <- as.character(num)
> char2
> char3 <- as.character(int)
> char3
> char4 <- as.character(comp)
> char4
> char5 <- as.character(logi)
> char5

Output:
Note: NA values have no type and therefore stay as such when converted to any type.

Summary

In this R tutorial, we learned about the various types of data that R supports. These data types are the basic building blocks of any kind of data in R programming. They assign meaning to values in R and also tell R how to process and how not to process them.

Now, you must go through our next tutorial on data structures in R to learn how to use these data types together.

Still, if you have any doubts related to the R data types tutorial, do let us know by leaving a comment below. We will be happy to solve them.

Exit mobile version