The data types in general words, the types of data attributes that tell the compiler (It can be an interpreter as well) how you are using the given data. In this article, you are going to know about the data types in the R programming language.
Data types in R programming
There are 5 basic data types available in R programming language.
- Numeric
- Integer
- Logical
- Complex
- Character
Let’s discuss each data types in R programming with an example for each. Let’s dive in…
1. Numeric data in R programming
In R programming a decimal value is called or considered as Numeric value. If you assign a value that has a decimal value to a variable, then it will be considered as numeric data.
Let’s see how it works!
#Variable with decimal value A = 1.0 #Prints the class of data / variable class(A)
"numeric"
Let’s see another example which shows the numeric data as the data class.
#Creates a variable with decimal values B <- c(1.2,4.4,5.0) #Returns the class of the data class(B)
"numeric"
As you can see in the above output, the decimal points in the R programming are considered as the Numeric data type and the variable is called numeric.
2. Integer data in R programming
The R language supports the Integer data type and any number without a decimal point will be considered as the integer data type in R programming.
Let’s see how it works.
You can make this in multiple ways. You can either call then as integer using as.integer function.
#Creates a variable as integer a <- as.integer(5) #Prints the class class(a)
"integer"
Well, let look over another methods i.e. by appending the L suffix to the variable. Let’s see how this works.
#Creates a variable by appending L b <- 25L #Prints the class of data class(b)
"integer"
3. Logical data in R programming
The R programming includes the Logical data types which will return either TRUE or FALSE about the given data type.
#Assigns the values to variables a <- 2 b <- 5 #Logic a > b
FALSE
a < b
TRUE
As you can see here, the logical data type which goes through the logic and returns logical values i.e. TRUE or FALSE as shown above.
we can try some other logics to understand logical data types better.
#Assigning the values a <- 2 b <- 5 #Returs true or false a == b
FALSE
In the above output, the value is less than b. But the logic asks is equal to b. You know that the logic is incorrect and it returns FALSE.
4. Complex data in R programming
The R language also supports the complex data type. The complex data type will include the imaginary part of the input number.
Let’s see how it works.
#Creates a complex number a <- 2+2i #Prints the class of data class(a)
Complex
You can also get the data type of the input value using the typeof() command in R language.
#Alternative command typeof(a)
"complex"
5. Character data in R programming
The final data type in R programming is the character data type. This data type includes the alphabets and special characters as well.
Let’s see some examples which illustrates the same.
#Creates a variable a <- "Hackanons.com" #prints the class class(a)
"Character"
Wrapping Up
We have 5 data types in R programming. Those are Numeric, Complex, Logical, Integer and Character data types.
The data types are very helpful as they let the compiler or the interpreter know about the data type that you are working on.
That’s all for now. Happy Learning!!!
More read: R documentation