Data types are an integral part of any programming language. They are one of the pioneer building blocks and the development of any programming language usually revolves around them. Consider data types as the oxygen of any programming language. Important for their existence. The same is the case with R Programming.
There are roughly six of the data types which have helped this programming language to evolve and blossom around the globe. Please find below listed are the data types R Programming language consists of.
In this article, we will go through all these five data types in-depth with hands-on example codes for better understanding.
The first thing that we should note before proceeding towards the data types is, how to create an atomic vector in R using a combined function. Because a vector is where you will initially store multiple values that you wanted to use somewhere in your program. Even if we define a variable with a single value, it is a vector.
Code illustration 1
Now, when we want to store multiple values under a variable, we have a combine function that allows us to store these values under the variable. All you need to do is store values under “c()”. See an example below:
Code illustration 2
Let’s move towards the data types as we now are aware of how to create a vector with multiple elements using a combine function.
The numeric data type is the most common data type in R. As the word itself suggests, numeric means anything that is a number value. However, under R, numeric has a resemblance with numbers without decimal as well as numbers with decimal. Both these values can be considered as numeric values and data that consist of a bunch of such values is called numeric data.
Code illustration 3
Well, if you see the screenshot above, there are two variables (vectors ideally) which we have created, numeric_decimal and num_without_decimal respectively. The first one contains the numeric values with decimal (also called floats in a programming language) and the second one consists of numeric values without decimal. A function named class() returns the data type of the vector passed as an argument under it. We can see that both these vectors are consisting of numeric types. We can say that, if one or more numbers are assigned to a variable under R, that variable will be of data type numeric. Unless and otherwise, we mention those with different data types specifically.
This might sound confusing to some people, but an integer is one of the numeric data types that doesn’t have decimals. However, it has separate denotation under R programming. If you want to define an integer numeric value under the R environment, you need to use “L” as a suffix after the numeric part. And where does that come in use you’ll ask. Well, imagine a situation where you wanted to store the number of employees in a vector so that it could be used for further analysis. The number of employees in an organization can’t be 3.5, or 1000. 56, right? Those are the situations where you’ll come up with integer data types.
Code illustration 4
There is another way to store values as integers under a variable. We have as.integer() function under R which allows us to store the value as integer under a specific variable and this approach seems to be more convenient to most of the people than using “L” as a suffix after a number. The reason is pretty simple, the later may create ambiguity and confusion for someone who is reading the code as it is a combination of a character and number.
Let’s see an example of how as.integer() work under R.
Code illustration 5
The as.integer() function takes a variable name as an argument. Therefore, you first need to create a variable with numeric values without decimals and then use that variable name as an argument under the as.integer() function.
Complex numbers have been an integral part of our life throughout academics. If you don’t remember some of them, let me take a moment to get an overview of a complex number in general. Mathematically, a complex number is a combination that can be expressed in terms of a + bi where bi is the imaginary part and a, b are the real numbers. R also supports a complex data type under its environment.
In the same manner, we can create imaginary numbers in R.
Code illustration 6
There is nothing much to discuss the complex data type. However, we still have a function called as.complex() that allows us to convert a numeric value provided as an argument to a complex data type.
Code illustration 7
The second most used data type you can say in any programming language is of character type also known as a string. There are two ways using which we can store and create a character data type under R; by either enclosing it under single quotes (‘ ’) or by enclosing it into double quotes (“ ”).
Code illustration 8
The interesting part of a character data type is, you can add almost anything under those double or single quotes to pop it out as a character. If we add numeric values under the double quotes, it will be considered as a character and not the number anymore. See an example as shown below:
Code illustration 9
Here, we have tried numeric, integer values as elements of a vector named num_as_char under double-quotes. Once you run the command that prints out the class of variable num_as_char, it results in character.
We also have a function called as.character() which allows us to convert a vector into a one with a character data type. This function will allow you to change any other type of data into character data.
Code illustration 10
Remember the variable int_var1? The one which we used to store the integer values? Well, in the code snippet above, we have tried it as an argument under as.character() function and stored its result under a new variable named int_as_char. You can see that int_as_char now belongs to the character class of the data.
There is again an interesting fact about character data types. If we add at least one single character value under a vector that consists of other data types, the entire vector gets converted to a character vector. See an example as shown below:
Code illustration 11
Any programming language is incomplete without the logical data type. We are having only two possible values for a logical data type: TRUE and FALSE. This type of data usually gets into the picture when we try to check some logical conditions. For Example, if two numbers are equal or not, if the first number is greater than the second number or not, and so on.
Code illustration 12
We can convert TRUE and FALSE values into numeric data types using as.numeric() function.
Code illustration 13
Conversely, numeric 1 and 0 can be converted into their logical counterparts TRUE and FALSE respectively using a built-in function named as.logical(). See an example below:
Code illustration 14
In this article, we tried to summarize the data types in the R Programming language. Their basic properties and also tried going hands-on with the help of some coding examples. Data types are the oxygen of any programming language, I repeat! No other programming language can be built before their existence. Numeric, Integer, Complex, Character, and Logical. We learned about these data types under the R environment in this session.
You can read out my previous article in this series of R Programming articles, that can be found on An Introduction to R Programming.
5 Factors Influencing Consumer Behavior
READ MOREElasticity of Demand and its Types
READ MOREAn Overview of Descriptive Analysis
READ MOREWhat is PESTLE Analysis? Everything you need to know about it
READ MOREWhat is Managerial Economics? Definition, Types, Nature, Principles, and Scope
READ MORE5 Factors Affecting the Price Elasticity of Demand (PED)
READ MORE6 Major Branches of Artificial Intelligence (AI)
READ MOREScope of Managerial Economics
READ MOREDijkstra’s Algorithm: The Shortest Path Algorithm
READ MOREDifferent Types of Research Methods
READ MORE
Latest Comments