In this tutorial you will learn about the R Data Types and its application with practical example.
R Data Types
Variables are used to represent reserved memory locations that is used to store values, when we create a variable we are a suppose to allocate some memory space for that variable. A variables always have a specific type and that type cannot change. Every variable have data type associated to it, the data type for a variable defines –
- The amount of memory space allocated for variables.
- A data type specifies the possible values for variables.
- The operations that can be performed on variables.
R comes with following built-in data types –
- Numeric
- Integer
- Complex
- Logical
- Character
R Numeric:- A variable with numeric data type is used to hold decimal values, if a decimal value is assigned to any variable then it will be referred to as numeric type.
Example:-
1 2 3 4 5 |
> num = 10.2 # assign a decimal value > num # print the value of num [1] 10.2 > class(num) # print the class name of num [1] "numeric" |
In R, even if we assign an integer value to a variable, it will still saved as a numeric type.
Example:-
1 2 3 4 5 6 7 |
> n = 5 > n # print the value of n [1] 5 > class(n) # print the class name of n [1] "numeric" > is.integer(n) # is n an integer? [1] FALSE |
R Integer:- Integers are used to store whole numbers. In R, an integer variable can be created using as.integer function, and integer data type can be verified using is.integer function.
Example:-
1 2 3 4 5 6 7 |
> i = as.integer(5) > i # print the value of i [1] 5 > class(i) # print the class name of i [1] "integer" > is.integer(i) # is i an integer? [1] TRUE |
Any numeric value passed to as.integer function is coerce into an integer value
Example:-
1 2 |
> as.integer(5.10) # coerce a numeric value [1] 5 |
R Complex:- R supports additional types for representing complex numbers (numbers with imaginary parts), to represent their real and imaginary parts. A complex value in R can be defined with pure imaginary value i.
Example:- Complex number can be defined as following –
1 2 3 4 5 |
> n = 3+7i # create a complex number > n # print the value of n [1] 3+7i > class(n) # print the class name of n [1] "complex" |
In R, as.complex function can also be used to coerce value to complex number.
R Logical:- The logical data type is used to represent the truth values, which can be either True or False. Logical are commonly created via comparison between variables.
Example:-
1 2 3 4 5 6 |
> a = 5; b = 10 > c = a > b > c [1] FALSE > class(c) [1] "logical" |
R Character:- A character variable is used to hold single or series of characters – letters, numbers, and special characters. It can be declared using double quotes “Hello World”.
Example:-
1 2 3 4 5 6 |
> name = "John" > name [1] "John" > class(name) [1] "character" > |
In R, as.character function can also be used to coerce value to character.
1 2 3 4 5 |
> num = as.character(3.15) > num # print the character string [1] "3.15" > class(num) # print the class name of num [1] "character" |