R Data Types
Data types are used in computer programming to specify the kind of data that can be stored in a variable. For effective memory consumption and precise computation, the right data type must be selected. Each data type has its own set of regulations and restrictions.
Data Types in R Programming Language
Each variable in R has an associated data type. Each R-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 R-data types and the following table shows the data type and the values that each data type can take.
|Basic Data Types|
|Set of all real numbers|
|Set of all integers, Z|
|TRUE and FALSE|
|Set of complex numbers|
|“a”, “b”, “c”, …, “@”, “#”, “$”, …., “1”, “2”, …etc|
Numeric Datatype in R
Decimal values are called numerics in R. It is the default R data type for numbers in R. If you assign a decimal value to a variable x as follows, x will be of numeric type. Real numbers with a decimal point are represented using this data type in R. it uses a format for double-precision floating-point numbers to represent numerical values.
 "numeric"  "double"
Even if an integer is assigned to a variable y, it is still saved as a numeric value.
 "numeric"  "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.
Integer Datatype in R
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 R data type.
 "integer"  "integer"  "integer"  "integer"
Logical Datatype in R
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. Boolean values, which have two possible values, are represented by this R data type: FALSE or TRUE
 TRUE  "logical"  "logical"
Complex Datatype in R
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.
 "complex"  "complex"
Character Datatype in R
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 data type is to wrap the value inside single or double inverted commas.
 "character"  "character"
There are several tasks that can be done using R 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.
Find data type of an object in R
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.
 "logical"  "integer"  "numeric"  "complex"  "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.
 TRUE  TRUE  TRUE  TRUE  TRUE  FALSE  FALSE
Coerce or convert the data type of an object to another
The process of altering the data type of an object to another type is referred to as coercion or data type conversion. This is a common operation in many programming languages that is used to alter data and perform various computations. When coercion is required, the language normally performs it automatically, whereas conversion is performed directly by the programmer.
Coercion can manifest itself in a variety of ways, depending on the R programming language and the context in which it is employed. In some circumstances, the coercion is implicit, which means that the language will change one type to another without the programmer having to expressly request it.
Note: All the coercions are not possible and if attempted will be returning an “NA” value.
 1  3+0i  TRUE  "1+2i"  NA Warning message: In print(as.numeric("12-04-2020")) : NAs introduced by coercion
Please Login to comment...