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:

> TRUE
[1] TRUE
> class(TRUE)
[1] "logical"
> FALSE
[1] FALSE
> class(FALSE)
[1] "logical"
> T
[1] TRUE
> F
[1] FALSE
> class(NA)
[1] "logical"

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.

> 7
[1] 7
> 7.8
[1] 7.8
> 7L
[1] 7
> 
> class(7)
[1] "numeric"
> class(7L)
[1] "integer"
 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.

> is.numeric(3)
[1] TRUE
> is.numeric(3L)
[1] TRUE
> is.integer(3)
[1] FALSE
> is.integer(3L)
[1] TRUE

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.

> "Welcome to R Studio"
[1] "Welcome to R Studio"
> 
> "I am a data scientist"
[1] "I am a data scientist"
> 
> class("Welcome to R Studio")
[1] "character"
> 
> class("I am a data scientist")
[1] "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.

> a<- double(6)
> a
[1] 0 0 0 0 0 0
> is.double(a)
[1] TRUE
> 
> b<- double(5)
> b
[1] 0 0 0 0 0
> is.double(b)
[1] TRUE

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.

> #Create a complex number
> z = 3 + 2i
> 
> #Print the class name of z
> class(z)
[1] "complex"

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.

> #Create a character vector
> name<- "Hello"
> 
> #convert character to raw
> r<- charToRaw(name)
> 
> #Print class name of r
> class(r)
[1] "raw"
> 
> #convert raw to character
> rawToChar(r)
[1] "Hello"
 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.

> #create a vector 'data' of length 2
> data <- raw(2)
> 
> data[1] <- as.raw(40)
> data[2] <- charToRaw("A")
> 
> #print the hexadecimal value
> data
[1] 28 41
> 
> #dput function writes an ASCII text representation of an R
> #object to a file or connection, or uses one to recreate the object
> dput(data)
as.raw(c(0x28, 0x41))
> 
> #convert vector 'data' to integer
> as.integer(data)
[1] 40 65

 

 

<< Previous     Next>>

 ;

Leave a Reply

Your email address will not be published. Required fields are marked *