Complex Data type 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.
# A simple R program
# to illustrate complex data type
# Assign a complex value to x
x = 4 + 3i
# print the class name of x
print(class(x))
# print the type of x
print(typeof(x))
Output
[1] "complex"
[1] "complex"
Character Data type 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.
# A simple R program
# to illustrate character data type
# Assign a character value to char
char = "GURUGRAM"
# print the class name of char
print(class(char))
# print the type of char
print(typeof(char))
Output
[1] "character"
[1] "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.
Raw data type in R
To save and work with data at the byte level in R, use the raw data
type. By displaying a series of unprocessed bytes, it enables low-
level operations on binary data. Here are some speculative data on
R’s raw data types:
R
# Create a raw vector
x <- as.raw(c(0x1, 0x2, 0x3, 0x4, 0x5))
print(x)
Output:
[1] 01 02 03 04 05
Five elements make up this raw vector x, each of which represents
a raw byte value.
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.
Syntax
class(object)
Example
# A simple R program
# to find data type of an object
# Logical
print(class(TRUE))
# Integer
print(class(3L))
# Numeric
print(class(10.5))
# Complex
print(class(1+2i))
# Character
print(class("12-04-2020"))
Output
[1] "logical"
[1] "integer"
[1] "numeric"
[1] "complex"
[1] "character"
Type verification
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.
Syntax:
is.data_type(object)
Example
# A simple R program
# Verify if an object is of a certain datatype
# Logical
print(is.logical(TRUE))
# Integer
print(is.integer(3L))
# Numeric
print(is.numeric(10.5))
# Complex
print(is.complex(1+2i))
# Character
print(is.character("12-04-2020"))
print(is.integer("a"))
print(is.numeric(2+3i))
Output
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] FALSE
[1] 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.
Syntax
as.data_type(object)
Note: All the coercions are not possible and if attempted will be
returning an “NA” value.
Example
# A simple R program
# convert data type of an object to another
# Logical
print(as.numeric(TRUE))
# Integer
print(as.complex(3L))
# Numeric
print(as.logical(10.5))
# Complex
print(as.character(1+2i))
# Can't possible
print(as.numeric("12-04-2020"))
Output
[1] 1
[1] 3+0i
[1] TRUE
[1] "1+2i"
[1] NA
Warning message:
In print(as.numeric("12-04-2020")) : NAs introduced by
coercion
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. R language is rich in built-
in operators and provides following types of operators.
Types of Operators
We have the following types of operators in R programming −
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Miscellaneous Operators
Arithmetic Operators
Following table shows the arithmetic operators supported by R
language. The operators act on each element of the vector.
Operat
Description Example
or
+ Adds two vectors Live Demo
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)
it produces the following result −
[1] 10.0 8.5 10.0
Live Demo
v <- c( 2,5.5,6)
− Subtracts second vector from the first t <- c(8, 3, 4)
print(v-t)
it produces the following result −
[1] -6.0 2.5 2.0
Live Demo
v <- c( 2,5.5,6)
* Multiplies both vectors t <- c(8, 3, 4)
print(v*t)
it produces the following result −
[1] 16.0 16.5 24.0
Live Demo
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
/ Divide the first vector with the second print(v/t)
When we execute the above code, it produces
the following result −
[1] 0.250000 1.833333 1.500000
Live Demo
v <- c( 2,5.5,6)
Give the remainder of the first vector
%% t <- c(8, 3, 4)
with the second print(v%%t)
it produces the following result −
[1] 2.0 2.5 2.0
Live Demo
v <- c( 2,5.5,6)
The result of division of first vector
%/% t <- c(8, 3, 4)
with second (quotient) print(v%/%t)
it produces the following result −
[1] 0 1 1
^ The first vector raised to the exponent Live Demo
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
of second vector print(v^t)
it produces the following result −
[1] 256.000 166.375 1296.000
Relational Operators
Following table shows the relational operators supported by R
language. Each element of the first vector is compared with the
corresponding element of the second vector. The result of
comparison is a Boolean value.
Operat
Description Example
or
Live Demo
Checks if each element of the first vector is v <- c(2,5.5,6,9)
> greater than the corresponding element of t <- c(8,2.5,14,9)
the second vector. print(v>t)
it produces the following result −
[1] FALSE TRUE FALSE FALSE
Live Demo
Checks if each element of the first vector is v <- c(2,5.5,6,9)
< less than the corresponding element of the t <- c(8,2.5,14,9)
second vector. print(v < t)
it produces the following result −
[1] TRUE FALSE TRUE FALSE
Live Demo
Checks if each element of the first vector is v <- c(2,5.5,6,9)
== equal to the corresponding element of the t <- c(8,2.5,14,9)
second vector. print(v == t)
it produces the following result −
[1] FALSE FALSE FALSE TRUE
Live Demo
Checks if each element of the first vector is v <- c(2,5.5,6,9)
<= less than or equal to the corresponding t <- c(8,2.5,14,9)
element of the second vector. print(v<=t)
it produces the following result −
[1] TRUE FALSE TRUE TRUE
Live Demo
Checks if each element of the first vector is v <- c(2,5.5,6,9)
>= greater than or equal to the corresponding t <- c(8,2.5,14,9)
element of the second vector. print(v>=t)
it produces the following result −
[1] FALSE TRUE FALSE TRUE
Live Demo
Checks if each element of the first vector is v <- c(2,5.5,6,9)
!= unequal to the corresponding element of the t <- c(8,2.5,14,9)
second vector. print(v!=t)
it produces the following result −
[1] TRUE TRUE TRUE FALSE