R Data Types
Data Types in R
There are many basic data types in R, which are of frequent occurrence in coding R calculations and programs. Though seemingly in the clear, they can at a halt deliver surprises. Here you will try to understand all of the different forms of data type well by direct testing with the R code.
Here is the list of all the data types provided by R:
Numeric Data Types
Decimal values are referred to as numeric data types in R. This is the default working out data type. If you assign a decimal value for any variable x like given below, x will become a numeric type.
> g = 62.4 # assign a decimal value to g > g # print the variable's value - g
Integer Data Type
If you want to create an integer variable in R, you have to invoke the as.integer() function to define any integer type data. You can be certain that y is definitely an integer by applying the is.integer() function.
> s = as.integer(3) > s # print the value of s
Fortunately, you can drive in a numeric value into an integer with this above mentioned as.integer() function like this:
as.integer(3.14) # drives in a numeric value
But it will work like type casting where the value of 3.14 gets changed to 3.
Complex Data Type
A complex value for coding in R can be defined using the pure imaginary values ‘i’.
> k = 1 + 2i # creating a complex number > k # printing the value of k
The below-mentioned example gives an error since −1 is not a complex value.
sqrt(−1) # square root of −1
And the error message will be something like this:
In sqrt(−1) : NaNs produced
Logical Data Type
A logical value is mostly created when a comparison between variables are done. An example will be like:
> a = 4; b = 6 # sample values > g = a > b # is a larger than b? > g # print the logical value
Character Data Type
A character object can be used for representing string values in R. You have to convert objects into character values using the as.character() function within your code like this:
> g = as.character(62.48) > g # prints the character string
 "3.14" > class(s) # print the class name of s