Before we delve deeper into R programming, it is important to understand the various data types used in R. R has the following basic data types:
Apart from these basic types, R also supports several data structures that are constructed from the basic types. These are:
This tutorial will help you understand the basic data types. The working of each complex data structure will be covered in more elaborate tutorials.
Numeric Data Types in R
Since R is predominantly a language designed for statistical purpose, numeric and integer data types are widely used. We will begin with the most frequently used numeric data type. As the name indicates, numeric corresponds to any numerical value. These values can be floating-point, decimal or double.
Before we assign our first variable, please note that the assignment operator in R is <- . Less than symbol followed by a hyphen.
Begin by assigning random numeric values to variables a, b, and c. The
typeof() function in R gives how the variable is stored internally, whereas the
class() function tells us what data type class this variable belongs to. R also offers some checking functions starting with “is“. The function
is.numeric() when called upon a variable returns a Boolean value of whether the object is of the numeric data type. There are respective is functions for other classes as well.
Run the following code in your script editor. Place the cursor on the first line and execute each line pressing Ctrl+ Enter. Also observe how these variables start to become a part of your environment window as you run each line.
a <- 3 b <- 5.4 c <- -9.5 typeof(a) typeof(b) typeof(c) is.integer(a) is.numeric(b) is.numeric(c) class(a)
> a <- 3 > b <- 5.4 > c <- -9.5 > typeof(a)  "double" > typeof(b)  "double" > typeof(c)  "double" > is.integer(a)  FALSE > is.numeric(b)  TRUE > is.numeric(c)  TRUE > class(a)  "numeric"
Note how all the variables are stored as double in the internal storage, irrespective of being defined with floating-point or not, as shown by the typeof() function. The variable a with value 3 is stored as 3.0, a double value. The function call is.integer() returns a FALSE Boolean value because of this. The
class(a) line gives our variable’s type numeric.
Data Types in R
Without any specification a numerical value is treated as a numeric data type by R compiler. Therefore, to define an integer variable, it is necessary to specify so. This is done by using the
a<-as.integer(a) typeof(a) is.integer(a) is.numeric(a)
Soon after you run the first line of the code, you see the value assigned to a become 3L, as opposed to 3. The suffix L signifies a long integer of range -2*10ˆ9 to +2*10ˆ9. The variable a is both an integer and a numeric, since all integers are numerics, but not vice versa.
> a<-as.integer(a) > typeof(a)  "integer" > is.integer(a)  TRUE > is.numeric(a)  TRUE
The character data type in R is meant to be used for both strings and single characters. A character can be simply specified by using a single or double quotes pair as below.
name="journaldev" typeof(name) name2='journaldev2' typeof(name2)
> name="journaldev" > typeof(name)  "character" > name2='journaldev2' > typeof(name2)  "character"
An integer or a numeric type can be converted into a character using as.character() function.
average=0.558 typeof(average) average=as.character(average) typeof(average)
> average=0.558 > typeof(average)  "double" > average=as.character(average) > typeof(average)  "character"
Logical is the data type used to store Boolean values TRUE and FALSE that arise as a result of logical operations. We have already encountered the TRUE and FALSE outputs above as we called the
is.integer() functions above. Let us verify this using the following statement.
> typeof(is.integer(average))  "logical"
Similarly, variables of logical type can be used to work with all logical operations.
x=TRUE; y=FALSE x&y #Logical AND x|y #Logical OR !x #Logical NOT !y #Logical NOT
> x=TRUE; y=FALSE > x&y  FALSE > x|y  TRUE > !x  FALSE > !y  TRUE
The final basic data type we discuss here is the complex type. This is used to represent complex numbers in mathematics. Complex numbers are of the form
a+bi where a and b are integers.
> complex=4+2i > typeof(complex)  "complex"
If we wish to calculate the square root of -1, which is mathematically known as i, the imaginary component of the imaginary number, We cannot calculate it by using normal
sqrt (square root) function.
typeof(sqrt(-1)) #Throws a warning message.
Warning message: In sqrt(-1) : NaNs produced
This is possible by defining -1 as a complex number first.
> sqrt(-1+0i)  0+1i > typeof(sqrt(-1+0i))  "complex"
This ends our discussion of the basic data types in R.