3 Operators & Data Objects
3.1 Operators
R has a wide range of operators for performing mathematical and logical operations. In this section, we’ll cover arithmetic operators, logical operators, comparison operators, and more.
3.1.1 Assignment Operators
Assignment operators are used to assigning values to various data objects in R. The objects may be integers, vectors, or functions. These values are then stored by the assigned variable names. There are two kinds of assignment operators: Left and Right
Left Assignment (\<-
or <<-
or =
)
Right Assignment (->
or ->>
)
The operators <-
and =
can be used, almost interchangeably, to assign to variable in the same environment.
The <<-
operator is used for assigning to variables in the parent environments (more like global assignments).
my_var <- 3
my_var <<- 3
3 -> my_var
my_var <<- 3
my_var # print my_var
<<-
OR ->>
is a global assigner.
Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.
To create a global variable inside a function, you can use the global assignment operator.
Additionally, use the global assignment operator if you want to change a global variable inside a function.
txt <- "awesome"
my_function <- function() {
txt <<- "fantastic"
paste("R is", txt)
}
my_function()
paste("R is", txt)
However, don’t worry much on function and the global variable, You will learn more about this in the preceding chapters.
3.1.2 Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Operators | Description |
+ | Addition |
_ | Subtraction |
* | Multiplication |
/ | Division |
^ | Exponent |
% | Modulus (Remainder from division) |
%/% | Integer Division |
3.1.3 Relational Operators
These are used for comparisons between values.
Each element of the first value is compared with the corresponding element of the second value.
The result of comparison is a Boolean value (TRUE/FALSE)
Operator | Description |
---|---|
== | Equal |
!= | Not equal |
> | Greater than |
< | Less than |
>= | greater than or equal |
<= | Less than or equal |
3 > 5
5 == 4
4 != 6
a <- "big"
b <- "small"
a == b
3.1.4 Logical Operators
It is applicable only to vectors of type logical, numeric or complex. All numbers greater than 1 are considered as logical value TRUE.
Each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.
Logical operators are used to combine conditional statements:
Operator | Name | Description |
---|---|---|
& | Element-wise Logical AND operator. | It returns TRUE if both elements are TRUE |
&& | Logical AND operator | Returns TRUE if both statements are TRUE |
| | Elementwise- Logical OR operator. | It returns TRUE if one of the statement is TRUE |
|| | Logical OR operator. | It returns TRUE if one of the statement is TRUE. |
! | Logical NOT | Returns FALSE if statement is TRUE |
3.2 Data Objects
At almost everything you will do in R is the concept that everything in R is an object.
Generally, while doing programming, we need to use variables to store various information. Variables are reserved memory locations to store values i.e when we create a variable we allocate some memory space. In R the variable is an object. An object is a data structure having few attributes and methods which are applied to its attributes.
3.2.1 Data Types
Character
A character object is represented by a collection of Alphabets,numbers and some symbols between double quotes (” “).
my_name <- "Africano"
as.character() # convert to character
Numeric
Numeric are used to represent continuous variables. E.g. are Time taken by all the athletes for completing a single lap in a race.
a <- 20.4
a <- as.numeric(20) # convert to numeric
Integer
Integers are natural numbers. They can be used to represent counting variables
a <- 20
a <- as.integer(20.345) # convert to integer
Complex
To represent complex numbers object of type complex is used. For creating object of type complex use as.complex()
or complex
functions.
as.complex(35 + (0+7i)) # convert to complex number
complex(real = 2, im = 1)
Logical
An object of data type logical can have the value TRUE or FALSE and is used to indicate if a condition is true or false.
as.logical() # convert to logical
3.2.2 Data Structures
Vectors
Used to store objects of the same type (think of them as arrays) Vector can be constructed using c()
vector() #declares an empty vector
a <- c(11, 25, 15.43, -0.6, -2, 4) #numeric vector
b <- c(TRUE, T, FALSE, F) # logical vector
c <- c("name", "x", "y") #character vector
a[1] #accessing the first element in vector a
b[1:4] #accessing elements from 1st to 4th
Lists
A list is like a vector.
However, an element of a list can be an object of any type and structure. Consequently, a list can contain another list and therefore it can be used to construct arbitrary data structures.
A list can also be constructed by using the function list()
id <- 1:4
month <- month.name
r <- seq(2, 3, 0.1)
my_list <- list(id, month, r)
print(my_list)
# accessing elements in a list
my_list[[1]]
my_list[[2]]
Factors
The factor data type is used to represent categorical data.
In factor variables mostly few possible values will be seen. These values are called LEVELS.
For example: variable ‘Gender’ with values male and female.
If a variable is having just two levels it is called as Dichotomous.
gender <- c("male", "male", "female", "male", "female")
gender <- as.factor(gender)
levels(Gender) # We can look at levels
satisfaction <- as.factor(c("Extremely poor", "bad", "moderate",
"good", "Extremely good", "Extremely poor", "good", "moderate"))
levels(satisfaction)
Matrices A matrix can be regarded as a generalization of a vector. All the elements of a matrix must be of the same data type.
i.e. Is a multi dimensional vector, they follow all the matrix algebraic operators and properties
mat1 <- matrix(nrow = 3, ncol = 3) # creating a 3 x 3 empty matrix
Data Frames Data frames can also be regarded as an extension to matrices.
Data frames can have columns of different data types and are the most convenient data structure for data analysis in R. In fact, most statistical modeling routines in R require a data frame as input.
Data frame can be created using data.frame()
Skill <- c("R", "SQL", "Excel")
Expert <- c("Africano", "Byamugisha", "Analyst 1")
df <- data.frame(Skill, Expert)
df
3.2.3 Mixing Objects
This brings in coercion. Coercion happens in R when the type of objects are changed during computation either implicitly or by using functions for explicit coercion
Notice that at first, x
is of type integer
. But when we assigned x[2] = "hi"
, all the elements of x
were coerced into character
as vectors in R can only hold data of single type.
Implicit Coercion Coercion happens with data types in R, often implicitly, so that the data can accommodate all the values, This is done by R with out the knowledge of the programmer.
Explicit Coercion In explicit coercion , The use can change one data type to another data type by applying function by the programmer.
# Examples of conversions
as.integer() # Converts the object to integer type
as.character()
as.factor()
as.data.frame()
as.numeric()
3.3 Tasks
- Can you spot the difference between a character string and a number? Here’s a test: Which of these are character strings and which are numbers?
1
,"1"
,"one"
. - Create an atomic vector of Character type, double type, logical type , integer type, complex type and raw type
- Check whether each of the created data structure is of vector type as well as check the class of each of the data.
- Create a List of heterogeneous data, which include numeric, character and logical vectors and print the lists.
- Create a matrix of 3 rows and 4 columns which stores data from 1 to 12 and arrange the value row wise.
- Create a Data Frame of 5 employees .