Data Types in R Programming

R Programming / Saturday, August 24th, 2019

Basic Data Types in R

As in all programming languages, data types play a vital role in developing any application and the data becomes very important especially in statistical analysis, to begin with, let’s discuss fundamental data types in R, which are also known as atomic vector types that are used extensively in R programs.

R uses a function named class( ) to determine the type of a variable. class( ) is used to determine the type of the variable TRUE. As TRUE is logical, the function class(TRUE) returns Logical. Similarly, class(NA) returns Logical where NA denotes the missing value. The logical variables TRUE and FALSE can also be abbreviated as T and F respectively.

 Question 01

Write the commands in R console to determine the type of the variable.

Solution:

Numeric and Integer

The numeric variables are used to represent numbers in R. Usually, all the arithmetic operations such as addition, subtraction, multiplication, and division can be directly applied on these numeric variables. Integer is a special type of numeric in R, which is used to represent natural numbers. To specify a digit as an integer, the programmer needs to add L next to it as shown in following code, but the users do not see the difference between the integer 7 and the numeric 7L from the output of view. However, the class( ) function reveals the difference for the inputs 7 and 7L.

 Question 02

Enumerate the process to check whether a given input is numeric or integer using a function in R.

Solution:

R provides a function for the programmers to check whether a given input is numeric or integer. This is known as the is-dot-function. Here, we use two functions – is.numeric( ) and is.integer( ) – as shown in the following code snippet , to check if a variable is a numeric or integer. In this particular case, it appears that both are numeric and the results also demonstrate the fact that all integer variables are numeric, but not all numeric variables are integers.

Character

R allows the programmers to use the string of characters.  The following code displays the class of character strings with the type of object as character.

Double

Apart from integer and numeric, R provides many other data types such as double for higher precision integer arithmetic, which creates a double-precision vector of the specified length. Initially, all the elements of the vector are initialized to 0. Following code snippet illustrates the double works.

Complex

Similarly, the following code illustrates another data type called complex for handling complex numbers, which has two parts – a real value and an imaginary value.

Raw

R provides flexibility to programmers to convert any given data types to a special data type called raw. The raw type is intended to hold any data as a sequence of bytes, where it is possible to extract sub-sequences of bytes and replace them as elements of a vector. In R, raw vectors are used to store fixed-length sequences of bytes. The following code shows how a character vector is converted into a raw vector and vice versa.

 Question 03

Write the commands in R to differentiate between the raw vectors and other vectors.

Solution:

Here, as shown in following code, a random vector of two bytes is initially created and stored as data; the first byte of data is assigned the value 40 and the second byte is assigned the character A. If the variable data is printed on the command prompt, it displays the hexadecimal equivalents of the stored values. Another function dput( ) is used to write an ASCII text representation of the variables. The variables can also be converted to other data types as in as.integer(data), which converts the variable data into integer and displays. However, this is used only for display and the actual data type of data remains as raw.

<< Previous Next>>