Classes and Manipulate Objects
R has a three-class system. These are S3, S4, and Reference Classes.
S3 Class
S3 is the simplest yet the most popular OOP system and it lacks formal definition and structure.
An object of this type can be created by just adding an attribute to it.
Example
# create a list with required components
nameList <- list(name = "Lal", country= "India")
# give a name to your class
class(nameList) <- "Address"
nameList
Output
$name
[1] "Lal"
$country
[1] "India"
attr(,"class")
[1] "Address"
S4 Class
Programmers of other languages like C++, Java might find S3 to be very much different than
their normal idea of classes as it lacks the structure that classes are supposed to provide. S4 is
a slight improvement over S3 as its objects have a proper definition and it gives a proper
structure to its objects.
# using setMethod to set a method
setMethod("show", "movies",
function(object)
{
cat("The name of the movie is ", object@name, ".\n")
cat(object@leadActor, "is the lead actor.\n")
}
)
movieList
[1] "show"
The name of the movie is Iron man .
Robert Downey Jr is the lead actor.
Reference Class
Reference Class is an improvement over S4 Class. Here the methods belong to the classes.
These are much similar to object-oriented classes of other languages. Defining a Reference
class is similar to defining S4 classes. We use setRefClass() instead of setClass() and “fields”
instead of “slots”.
library(methods)
# setRefClass returns a generator
movies <- setRefClass("movies", fields = list(name = "character",
leadActor = "character", rating = "numeric"))
#now we can use the generator to create objects
movieList <- movies(name = "Iron Man",
leadActor = "Robert downey Jr", rating = 7)
movieList
Reference class object of class "movies"
Field "name":
[1] "Iron Man"
Field "leadActor":
[1] "Robert downey Jr"
Field "rating":
[1] 7
Manipulate Objects
Every programming language has its own data types to store values or any information so that
the user can assign these data types to the variables and perform operations respectively.
Operations are performed accordingly to the data types. These data types can be character,
integer, float, long, etc. Based on the data type, memory/storage is allocated to the variable.
For example, in C language character variables are assigned with 1 byte of memory, integer
variable with 2 or 4 bytes of memory and other data types have different memory allocation
for them. variables are assigned to objects rather than data types in R programming.
Vectors
Lists
Data frames
Matrices
Arrays
Factors
Tibbles
Control Statements If, Else and Switch
Control statements are expressions used to control the execution and flow of the program based
on the conditions provided in the statements. These structures are used to make a decision after
assessing the variable. In this article, we’ll discuss all the control statements with the examples.
In R programming, there are 8 types of control statements as follows:
if condition
if-else condition
for loop
nested loops
while loop
repeat and break statement
return statement
next statement
if condition
This control structure checks the expression provided in parenthesis is true or not. If true, the
execution of the statements in braces {} continues.
Syntax:
if(expression){
statements
....
....
}
Example:
x <- 100
if(x > 10){
print(paste(x, "is greater than 10"))
}
Output:
[1] "100 is greater than 10"
if-else condition
It is similar to if condition but when the test expression in if condition fails, then statements in
else condition are executed.
Syntax:
if(expression){
statements
....
....
}
else{
statements
....
....
}
Example:
x <- 5
# Check value is less than or greater than 10
if(x > 10){
print(paste(x, "is greater than 10"))
}else{
print(paste(x, "is less than 10"))
}
Output:
[1] "5 is less than 10"
for loop
It is a type of loop or sequence of statements executed repeatedly until exit condition is reached.
Syntax:
for(value in vector){
statements
....
....
}
Example:
x <- letters[4:10]
for(i in x){
print(i)
}
Output:
[1] "d"
[1] "e"
[1] "f"
[1] "g"
[1] "h"
[1] "i"
[1] "j"
Nested loops
Nested loops are similar to simple loops. Nested means loops inside loop. Moreover, nested
loops are used to manipulate the matrix.
Example:
# Defining matrix
m <- matrix(2:15, 2)
for (r in seq(nrow(m))) {
for (c in seq(ncol(m))) {
print(m[r, c])
}
}
Output:
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10
[1] 12
[1] 14
[1] 3
[1] 5
[1] 7
[1] 9
[1] 11
[1] 13
[1] 15
while loop
while loop is another kind of loop iterated until a condition is satisfied. The testing expression
is checked first before executing the body of loop.
Syntax:
while(expression){
statement
....
....
}
Example:
x=1
# Print 1 to 5
while(x <= 5){
print(x)
x=x+1
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
repeat loop and break statement
repeat is a loop which can be iterated many number of times but there is no exit condition to
come out from the loop. So, break statement is used to exit from the loop. break statement can
be used in any type of loop to exit from the loop.
Syntax:
repeat {
statements
....
....
if(expression) {
break
}
}
Example:
x=1
# Print 1 to 5
repeat{
print(x)
x=x+1
if(x > 5){
break
}
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
return statement
return statement is used to return the result of an executed function and returns control to the
calling function.
Syntax:
return(expression)
Example:
# Checks value is either positive, negative or zero
func <- function(x){
if(x > 0){
return("Positive")
}else if(x < 0){
return("Negative")
}else{
return("Zero")
}
}
func(1)
func(0)
func(-1)
Output:
[1] "Positive"
[1] "Zero"
[1] "Negative"
next statement
next statement is used to skip the current iteration without executing the further statements and
continues the next iteration cycle without terminating the loop.
Example:
# Defining vector
x <- 1:10
# Print even numbers
for(i in x){
if(i%%2 != 0){
next #Jumps to next loop
}
print(i)
}
Output:
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10