R – Data Types

23 Feb, 2022
Each variable in R has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. R Programming language has the following basic data types and the following table shows the data type and the values that each data type can take.
Decimal values are called numerics in R. It is the default data type for numbers in R. If you assign a decimal value to a variable x as follows, x will be of numeric type.
R
# A simple R program# to illustrate Numeric data type # Assign a decimal value to xx = 5.6 # print the class name of variableprint(class(x)) # print the type of variableprint(typeof(x))
Output: 
[1] "numeric"
[1] "double"
Even if an integer is assigned to a variable y, it is still being saved as a numeric value.
R
# A simple R program# to illustrate Numeric data type # Assign an integer value to yy = 5 # print the class name of variableprint(class(y)) # print the type of variableprint(typeof(y))
Output: 
[1] "numeric"
[1] "double"
When R stores a number in a variable, it converts the number into a “double” value or a decimal type with at least two decimal places. This means that a value such as “5” here, is stored as 5.00 with a type of double and a class of numeric. And also y is not an integer here can be confirmed with the is.integer() function. 
R
# A simple R program# to illustrate Numeric data type # Assign a integer value to yy = 5 # is y an integer?print(is.integer(y))
Output: 
[1] FALSE
R supports integer data types which are the set of all integers. You can create as well as convert a value into an integer type using the as.integer() function. You can also use the capital ‘L’ notation as a suffix to denote that a particular value is of the integer data type.
R
# A simple R program# to illustrate integer data type # Create an integer valuex = as.integer(5) # print the class name of xprint(class(x)) # print the type of xprint(typeof(x)) # Declare an integer by appending an L suffix.y = 5L # print the class name of yprint(class(y)) # print the type of yprint(typeof(y))
Output: 
[1] "integer"
[1] "integer"
[1] "integer"
[1] "integer"
R has logical data types that take either a value of true or false. A logical value is often created via a comparison between variables. 
R
# A simple R program# to illustrate logical data type # Sample valuesx = 4y = 3 # Comparing two valuesz = x > y # print the logical valueprint(z) # print the class name of zprint(class(z)) # print the type of zprint(typeof())
Output: 
[1] TRUE
[1] "logical"
[1] "logical"
R supports complex data types that are set of all the complex numbers. The complex data type is to store numbers with an imaginary component. 
R
# A simple R program# to illustrate complex data type # Assign a complex value to xx = 4 + 3i # print the class name of xprint(class(x)) # print the type of xprint(typeof(x))
Output: 
[1] "complex"
[1] "complex"
R supports character data types where you have all the alphabets and special characters. It stores character values or strings. Strings in R can contain alphabets, numbers, and symbols. The easiest way to denote that a value is of character type in R is to wrap the value inside single or double inverted commas. 
R
# A simple R program# to illustrate character data type # Assign a character value to charchar = "Geeksforgeeks" # print the class name of charprint(class(char)) # print the type of charprint(typeof(char))
Output: 
[1] "character"
[1] "character"
There are several tasks that can be done using data types. Let’s understand each task with its action and the syntax for doing the task along with an R code to illustrate the task.  
To find the data type of an object you have to use class() function. The syntax for doing that is you need to pass the object as an argument to the function class() to find the data type of an object.
Syntax:  
class(object)
Example: 
R
# A simple R program# to find data type of an object # Logicalprint(class(TRUE)) # Integerprint(class(3L)) # Numericprint(class(10.5)) # Complexprint(class(1+2i)) # Characterprint(class("12-04-2020"))
Output: 
[1] "logical"
[1] "integer"
[1] "numeric"
[1] "complex"
[1] "character"
To do that, you need to use the prefix “is.” before the data type as a command. The syntax for that is, is.data_type() of the object you have to verify.
Syntax: 
is.data_type(object)
Example:  
R
# A simple R program# Verify if an object is of a certain datatype # Logicalprint(is.logical(TRUE)) # Integerprint(is.integer(3L)) # Numericprint(is.numeric(10.5)) # Complexprint(is.complex(1+2i)) # Characterprint(is.character("12-04-2020")) print(is.integer("a")) print(is.numeric(2+3i))
Output: 
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
This task is interesting where you can change or convert the data type of one object to another. To perform this action you have to use the prefix “as.” before the data type as the command. The syntax for doing that is as.data_type() of the object which you want to coerce.
Syntax:  
as.data_type(object) 
Note: All the coercions are not possible and if attempted will be returning an “NA” value.
Example:  
R
# A simple R program# convert data type of an object to another # Logicalprint(as.numeric(TRUE)) # Integerprint(as.complex(3L)) # Numericprint(as.logical(10.5)) # Complexprint(as.character(1+2i)) # Can't possibleprint(as.numeric("12-04-2020"))
Output: 
[1] 1
[1] 3+0i
[1] TRUE
[1] "1+2i"
[1] NA
Warning message:
In print(as.numeric("12-04-2020")) : NAs introduced by coercion
kumar_satyam
ishmartishtiaq709
Picked
R Data-types
Programming Language
R Language
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.