R Programming
R Programming
There are many types of R-objects. The frequently used ones are −
Vectors
Lists
Matrices
Arrays
Factors
Data Frames
The simplest of these objects is the vector object and there are six data
types of these atomic vectors, also termed as six classes of vectors. The
other R-Objects are built upon the atomic vectors.
Vectors
When you want to create vector with more than one element, you should
use c() function which means to combine the elements into a vector.
Live Demo
# Create a vector.
apple <- c('red','green',"yellow")
print(apple)
Lists
A list is an R-object which can contain many different types of elements
inside it like vectors, functions and even another list inside it.
Live Demo
# Create a list.
list1 <- list(c(2,5,3),21.3,sin)
[[2]]
[1] 21.3
[[3]]
function (x) .Primitive("sin")
Matrices
A matrix is a two-dimensional rectangular data set. It can be created
using a vector input to the matrix function.
Live Demo
# Create a matrix.
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)
When we execute the above code, it produces the following result −
[,1] [,2] [,3]
[1,] "a" "a" "b"
[2,] "c" "b" "a"
Arrays
While matrices are confined to two dimensions, arrays can be of any
number of dimensions. The array function takes a dim attribute which
creates the required number of dimension. In the below example we
create an array with two elements which are 3x3 matrices each.
Live Demo
# Create an array.
a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)
When we execute the above code, it produces the following result −
,,1
Factors
Factors are created using the factor() function. The nlevels functions
gives the count of levels.
Live Demo
# Create a vector.
apple_colors <- c('green','green','yellow','red','red','red','green')
Data Frames
Data frames are tabular data objects. Unlike a matrix in data frame each
column can contain different modes of data. The first column can be
numeric while the second column can be character and third column can
be logical. It is a list of vectors of equal length.
Data Frames are created using the data.frame() function.
Live Demo
# Create the data frame.
BMI <- data.frame(
gender = c("Male", "Male","Female"),
height = c(152, 171.5, 165),
weight = c(81,93, 78),
Age = c(42,38,26)
)
print(BMI)
When we execute the above code, it produces the following result −
gender height weight Age
1 Male 152.0 81 42
2 Male 171.5 93 38
3 Female 165.0 78 26
R PACKAGE
The most common method of installing and loading packages is using
the install.packages() and library() function respectively. Let us see a brief about
these functions –
Install.packages() is used to install a required package in the R programming
language.
Syntax:
install.packages(“package_name”)
library() is used to load a specific package in R programming language
Syntax:
library(package_name)
In the case where multiple packages have to installed and loaded these commands
have to be specified repetitively. Thus making the approach inefficient.
install.packages("ggplot2")
install.packages("dpylr")
install.packages("readxl")
library(ggplot2)
library(dpylr)
library(readxl)
The most efficient way to install the R packages is by installing multiple packages at a
time using. For installing multiple packages we need to use install.packages( ) function
again but this time we can pass the packages to be installed as a vector or a list with
each package separated by comma(,).
Syntax :
install.packages ( c(“package 1″ ,”package 2”, . . . . , “package n”) )
install.packages(“package1″,”package2”, . . . . , “package n”)
Example :
install.packages(c("ggplot2","dpylr","readxl"))
install.packages("ggplot2","dpylr","readxl")
library("ggplot2","dpylr")
pacman::p_load(dplyr,ggplot2,readxl)
Operators in R
There are the following types of operators used in R:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Miscellaneous Operators
Arithmetic Operators
Arithmetic operators are the symbols which are used to represent arithmetic
math operations. The operators act on each and every element of the vector.
There are various arithmetic operators which are supported by R
[1] 0 0 4
Logical Operators
The logical operators allow a program to make a decision on the basis of
multiple conditions. In the program, each operand is considered as a
condition which can be evaluated to a false or true value. The value of the
conditions is used to determine the overall value of the op1 operator op2.
Logical operators are applicable to those vectors whose type is logical,
numeric, or complex.
The logical operator compares each element of the first vector with the
corresponding element of the second vector.
There are the following types of operators which are supported by R:
1. & This operator is known as the Logical AND a <- c(3, 0, TRUE,
2+2i)
operator. This operator takes the first element
b <- c(2, 4, TRUE,
of both the vector and returns TRUE if both the
elements are TRUE. 2+3i)
print(a&b)
[1] TRUE
FALSE TRUE TRUE
[1] TRUE
TRUE TRUE TRUE
[1] FALSE
TRUE FALSE FALSE
4. && This operator takes the first element of both the a <- c(3, 0, TRUE,
2+2i)
vector and gives TRUE as a result, only if both
b <- c(2, 4, TRUE,
are TRUE. 2+3i)
print(a&&b)
[1] TRUE
5. || This operator takes the first element of both the a <- c(3, 0, TRUE,
2+2i)
vector and gives the result TRUE, if one of them
b <- c(2, 4, TRUE,
is true. 2+3i)
print(a||b)
[1] TRUE
Assignment Operators
An assignment operator is used to assign a new value to a variable. In R,
these operators are used to assign values to vectors. There are the following
types of assignment
[1] 1 2 3 4 5 6 7
8
[1] FALSE
[1] FALSE
14 32
32 77
R Switch Statement
A switch statement is a selection control mechanism that allows the value of
an expression to change the control flow of program execution via map and
search.
The switch statement is used in place of long if statements which compare a
variable with several integral values. It is a multi-way branch statement
which provides an easy way to dispatch execution for different parts of code.
This code is based on the value of the expression.
This statement allows a variable to be tested for equality against a list of
values. A switch statement is a little bit complicated. To understand it, we
have some key points which are as follows:
o If expression type is a character string, the string is matched to the listed
cases.
o If there is more than one match, the first match element is used.
o No default case is available.
o If no case is matched, an unnamed case is used.
There are basically two ways in which one of the cases is selected:
1) Based on Index
If the cases are values like a character vector, and the expression is
evaluated to a number than the expression's result is used as an index to
select the case.
Example 1
x <- switch(
3,
"Shubham",
"Nishka",
"Gunjan",
"Sumit"
)
print(x)
Output:
PROGRAM:
week < - c('Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday')
for (day in week)
{
# displaying each string in the vector
print(day)
}
Output:
[1] "Sunday"
[1] "Monday"
[1] "Tuesday"
[1] "Wednesday"
[1] "Thursday"
[1] "Friday"
[1] "Saturday"
In the above program, initially, all the days(strings) of the week are assigned to
the vector week. Then for loop is used to iterate over each string in a week. In
each iteration, each day of the week is displayed.
WHILE LOOP
While loop is used when the exact number of iterations of loop is not known
beforehand. It executes the same code again and again until a stop condition is
met. While loop checks for the condition to be true or false n+1 times rather
than n times. This is because the while loop checks for the condition before
entering the body of the loop.
When the above code is compiled and executed, it produces the following result −
[1] "Hello" "while loop"
[1] "Hello" "while loop"
[1] "Hello" "while loop"
[1] "Hello" "while loop"
[1] "Hello" "while loop"
LOOPING OVER LIST:
Looping over a list is just as easy and convenient as looping over a
vector. Looping over list has 3 ways
output
[1] "Mavericks"
[1] "G" "F" "C"
[1] 3
.
output
[1] "Mavericks"
[1] "G"
[1] "F"
[1] "C"
[1] 3
3: Loop Through List & Only Display Specific Values
output
[1] "Mavericks"
[1] "G"
[1] 3
Types of vectors
Atomic vectors in R
In R, there are four types of atomic vectors. Atomic vectors play an important role in Data
Science. Atomic vectors are created with the help of c() function. These atomic vectors are
as follows:
Numeric vector
The decimal values are known as numeric data types in R. If we assign a decimal value to
any variable d, then this d variable will become a numeric type. A vector which contains
numeric elements is known as a numeric vector.
Example:
1. d<-45.5
2. num_vec<-c(10.1, 10.2, 33.2)
3. class(d)
4. num_vec
5. class(d)
6. class(num_vec)
Output
[1] 45.5
[1] 10.1 10.2 33.2
[1] "numeric"
[1] "numeric"
Integer vector
A non-fraction numeric value is known as integer data. This integer data is represented by
"Int." The Int size is 2 bytes and long Int size of 4 bytes. There is two way to assign an
integer value to a variable, i.e., by using as.integer() function and appending of L to the
value.
A vector which contains integer elements is known as an integer vector.
Example:
1. d<-as.integer(5)
2. e<-5L
3. int_vec<-c(1,2,3,4,5)
4. int_vec<-as.integer(int_vec)
5. int_vec1<-c(1L,2L,3L,4L,5L)
6. class(d)
7. class(e)
8. class(int_vec)
9. class(int_vec1)
Output
[1] "integer"
[1] "integer"
[1] "integer"
[1] "integer"
Character vector
A character is held as a one-byte integer in memory. In R, there are two different ways to
create a character data type value, i.e., using as.character() function and by typing string
between double quotes("") or single quotes('').
A vector which contains character elements is known as an integer vector.
Example:
1. d<-'shubham'
2. e<-"Arpita"
3. f<-“65”
4. f<-as.character(f)
5. d
6. e
7. f
8. char_vec<-c(1,2,3,4,5)
9. char_vec<-as.character(char_vec)
10. char_vec1<-c("shubham","arpita","nishka","vaishali")
11. char_vec
12. class(d)
13. class(e)
14. class(f)
15. class(char_vec)
16. class(char_vec1)
Output
[1] "shubham"
[1] "Arpita"
[1] "65"
[1] "1" "2" "3" "4" "5"
[1] "shubham" "arpita" "nishka" "vaishali"
[1] "character"
[1] "character"
[1] "character"
[1] "character"
[1] "character"
Logical vector
The logical data types have only two values i.e., True or False. These values are based on
which condition is satisfied. A vector which contains Boolean values is known as the logical
vector.
Example:
1. d<-as.integer(5)
2. e<-as.integer(6)
3. f<-as.integer(7)
4. g<-d>e
5. h<-e<f
6.
7. log_vec<-c(d<e, d<f, e<d,e<f,f<d,f<e)
8. log_vec
9. class(g)
10. class(h)
11. class(log_vec)
Output
[1] FALSE
[1] TRUE
[1] TRUE TRUE FALSE TRUE FALSE FALSE
[1] "logical"
[1] "logical"
[1] "logical"
Vector subsetting
In R Programming Language, subsetting allows the user to access elements from an object. It
takes out a portion from the object based on the condition provided. There are 4 ways of
subsetting in R programming. Each of the methods depends on the usability of the user and the
type of object.
Program
# Create vector
x <- 1:15
# Print vector
cat("Original vector: ", x, "\n")
# Subsetting vector
cat("First 5 values of vector: ", x[1:5], "\n")
[[ ]] operator is used for subsetting of list-objects. This operator is the same as [ ] operator but the
only difference is that [[ ]] selects only one element whereas [ ] operator can select more than 1
element in a single command.
# Create list
ls <- list(a = 1, b = 2, c = 10, d = 20)
# Print list
cat("Original List: \n")
print(ls)
$b
[1] 2
$c
[1] 10
$d
[1] 20
$ operator can be used for lists and data frames in R. Unlike [ ] operator, it selects only a single
observation at a time. It can be used to access an element in named list or a column in data
frame. $ operator is only applicable for recursive objects or list-like objects.
# Create list
ls <- list(a = 1, b = 2, c = "Hello", d = "GFG")
# Print list
cat("Original list:\n")
print(list)
$b
[1] 2
$c
[1] "Hello"
$d
[1] "GFG"
Using $ operator:
[1] "GFG"
subset() function in R programming is used to create a subset of vectors, matrices, or data frames
based on the conditions provided in the parameters.
Syntax: subset(x, subset, select)
Parameters:
x: indicates the object
subset: indicates the logical expression on the basis of which subsetting has to be done
select: indicates columns to select
R Data Frame
A data frame is a two-dimensional array-like structure or a table in which a column contains
values of one variable, and rows contains one set of values from each column. A data frame
is a special case of the list in which each component has equal length.
A data frame is used to store data table and the vectors which are present in the form of a
list in a data frame, are of equal length.
In a simple way, it is a list of equal length vectors. A matrix can contain one type of data, but
a data frame can contain different data types such as numeric, character, factor, etc.
There are following characteristics of a data frame.
R Lists
In R, lists are the second type of vector. Lists are the objects of R which contain elements of
different types such as number, vectors, string and another list inside it. It can also contain a
function or a matrix as its elements. A list is a data structure which has components of
mixed data types. We can say, a list is a generic vector which contains other objects.
Example
vec <- c(3,4,5,6)
char_vec<-c("shubham","nishka","gunjan","sumit")
logic_vec<-c(TRUE,FALSE,FALSE,TRUE)
out_list<-list(vec,char_vec,logic_vec)
Output:
[[1]]
[1] 3 4 5 6
[[2]]
[1] "shubham" "nishka" "gunjan" "sumit"
[[3]]
[1] TRUE FALSE FALSE TRUE
Lists creation
The process of creating a list is the same as a vector. In R, the vector is created with the
help of c() function. Like c() function, there is another function, i.e., list() which is used to
create a list in R. A list avoid the drawback of the vector which is data type. We can add the
elements in the list of different data types.
syntax
list()
Example 1: Creating list with same data type
list_1<-list(1,2,3)
list_2<-list("Shubham","Arpita","Vaishali")
list_3<-list(c(1,2,3))
list_4<-list(TRUE,FALSE,TRUE)
list_1
list_2
list_3
list_4
Output:
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[1]]
[1] "Shubham"
[[2]]
[1] "Arpita"
[[3]]
[1] "Vaishali"
[[1]]
[1] 1 2 3
[[1]]
[1] TRUE
[[2]]
[1] FALSE
[[3]]
[1] TRUE