In this blog we will learn the basic data types in R programming. Before proceeding to data types, let’s do the recap of our previous article
We have already done the environment setup for using R. Even though if you have missed it, here is a link Getting started with R programming.
Above link will guide you through the environment setup and installations steps.
Now we are done with the environment setup, it is time for us to deep dive into the exciting world of R programming.
Before getting inside any programming languages we must know the basic data types associated with variables.
Basic data types are as given below:
-
Int – Integer
-
Char – Character
-
Float- Floating point precision value
-
Double – double precision floating point value
These are the basic data types. Now we are concerned about the data types in R. below is the detailed explanation of Data types in R and their basic implementation:
Data types in R:
There are basically 5 data types in R programming language:
-
Int
-
Double
-
Char
-
Complex
-
Logical
Let’s see one by one.
Note: we are going to work in R studio for every tutorial. To execute the statement use “Ctrl + Enter”
-
Int (Integer)
Int is nothing but the integer data type as we have seen earlier. Any numeric value (except fractional ex. 0.1) comes under the category of int.
Declaring an integer variable in R:
Let x be our variable.
x <- 10L
(less than ‘<’ followed by hyphen ‘-’ is assignment operator in R )
Press ctrl + enter to execute the above statement.
It will store 10 into the x variable. Now you might be thinking about the L part in the declaration statement. We will discuss it in some time. Below is the code snippet:
#Integer a <- 2L
after executing the above statement(pressing ctrl+Enter) the code gets copied and execute in the console and we get the result in Global Environment.
-
Double
Double just basically means the value with decimal point
#Double b <- 39.5
Again you need to press ctrl+Enter to execute the statement and it will store the value into variable b as Double.
Now you might have observed that we didn’t use ‘L’ for double values. Reason for this is explained below
By default, R decides that in which format it is going to store the data. All arithmetic operations in R, are conducted in the double data type.
Hence, it is necessary to put ‘L’ in front of a value so that it will understand that the value is of Integer data type.
Next data type is quite interesting. If you love mathematics then you will definitely love this data type
-
Complex
Complex data type is used to store the complex numbers.
#Complex Complex <- 3+2i
Now after executing the statement 3+2i will be assigned to the variable Complex.
-
Char
Char in R is used to store the character as well as series of multiple characters which we basically refer as string. So, in R we don’t really need another data type to store value of type String. Below is an example.
#Character C <- ‘r’ C1 <- ‘R Programming’
-
Logical
Logical data type is the one which stores values such as True or False.
You can use the syntax given below to store the logical values.
#Logical Logical1 <- T Logical2 <- F Logical3 <- True Logical4 <- False
Below is the snapshot of the implementation of all the data types we listed above.
Image: Data Types
Image: Console
Image: Global Environment
Note: Please make sure of the case if you are using T as True.
Using “Logical <- t” will give you another result which is not really important for us at this moment.
Now if you want to check the data type of variables you have declared above,
You can use the below function:
typeof(object)
Example:
-
Integer
X <- 20L
typeof(X)
-
Double
Y <- 20.5
typeof(Y)
-
Complex
C <- 10+5i
typeof(C)
-
Character
Char <- ‘Character’
typeof(Char)
-
Logical
L <- T typeof(L) L1 <- F typeof(L1) L3 <- True typeof(L2) L4 <- False typeof(L4)
Here is the snapshot showing the use of typeof() function of R.
So, we have seen the data types and their implementation.
Also we have seen the use of typeof().
Keep in touch for more articles like this. If you like this article please let me know. Also if you think the article is not helpful, feel free to give feedback by filling up the form below. It will help me to come with more improved content.
Keep coding.
Feedback
[wpforms id=”22″ title=”false”]