In programming, different types of information are stored at each memory location. When a variable is created, its memory space is reserved according to its data type.
The values of integer, float, boolean or character type are stored on the variable.
Data Types of R Language are given below.
- Numeric
- Integer
- Complex
- Logical
- Character
1. Numeric
All numbers in R language are ‘numeric data types’. The data type of all these numeric values is by default ‘numeric’.
Source Code :
> var1 = 5 > var2 = 10.5 > class(var1) [1] "numeric" > class(var2) [1] "numeric"
Check Numeric Value is Integer or Not in R
In most programming languages, the number that does not have a fractional part is the value of the Integer data type. But every value given by default in R; is numeric data type.
For example, whether the given value is an integer or not, it is checked.
Source Code :
var = 5 > is.integer(var) [1] FALSE
2. Integer Data Type
In most programming languages, the numeric values that do not have a fractional part are Integers. But in R language if you want to convert numeric value to integer then integer() function is used.
Convert ‘numeric’ to ‘integer’ Data Type in R
Source Code :
var = 5 > as.integer(var) [1] 5 > class(as.integer(var)) [1] "integer"
3. Complex Data Type
Complex numbers in R language have two parts, one part is ‘real’ and the other part is ‘imaginary’. For Example, 5+2i
Example for Complex Data Type in R
Source Code :
> var = 5+2i > class(var) [1] "complex"
4. Logical Data Type
When comparison is done between two operands in R language, then logical values are created. For Example, TRUE and FALSE
Example for Logical Data Type in R
Source Code :
> var1 = 5 > var2 = 5 > var1 == var2 [1] TRUE > var1 < var2 [1] FALSE
5. Character Data Type
Character Data Type is used to describe String Values. The character(s) is written in single or double quotes.
Example for Character Data Type in R
Source Code :
> var = "Hello World" > class(var) [1] "character"
Example for Convert into Character Data Type in R
The as.character() function is used to convert to character data type.
Source Code :
> var = 5 > as.character(var) [1] "5"
Example for Check character or not in R
Source Code :
> var = "5" > is.character(var) [1] TRUE > var = 5 > is.character(var) [1] FALSE