0% found this document useful (0 votes)
11 views69 pages

DSR Unit 2

The document provides a comprehensive guide on using R for data science, covering installation, basic math operations, variable management, and data types. It explains R sessions, functions, and the importance of vectors, including their creation and manipulation. Additionally, it highlights the case sensitivity of variable names and the rules governing data types within vectors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views69 pages

DSR Unit 2

The document provides a comprehensive guide on using R for data science, covering installation, basic math operations, variable management, and data types. It explains R sessions, functions, and the importance of vectors, including their creation and manipulation. Additionally, it highlights the case sensitivity of variable names and the rules governing data types within vectors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

DATA SCIENCE USING R

VIII SEMESTER
DS-427T
UNIT-2
Department of Computer Science and Engineering,
BVCOE New Delhi
1 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
How to Run R
1. Download, Install R (from the Berkely server): https://cran.cnr.berkeley.edu
2. Download, Install RStudio: https://www.rstudio.com/products/rstudio/download3/
3. Run RStudio Just the R Console appears when the R app is run by itself, that is, not
within the RStudio environment. The figure below shows what opens the first time
RStudio is run. You get the same R Console from the R app, and you get more. You are
running R either way.

Department of Computer Science and Engineering,


BVCOE New Delhi
2 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
R Sessions and Functions
R Sessions:- R is a case-sensitive, interpreted language. You can enter commands one at a
time at the command prompt (>) or run a set of commands from a source file.
There are a wide variety of data types, including vectors, matrices, data frames (similar to
datasets), and lists (collections of objects).
The standard assignment operator in R is <-. = can also used, but this is discouraged, as it
does not work in some special situations.
There are no fixed types associated with variables.
The variables can be printed without any print statement by giving name of the variable.
y <- 5
> y # print out y [1] 5
>print y # print out y [1] 5
Comments (#) are especially valuable for documenting program code, but they are useful in
interactive sessions.
Note:- Prompt for new input is „>‟
„+‟ is a line continuation character inR.

Department of Computer Science and Engineering,


BVCOE New Delhi
3 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
R Sessions and Functions
Functions:- A function is a group of instructions that takes inputs, uses them to compute
other values, and returns a result.

Department of Computer Science and Engineering,


BVCOE New Delhi
4 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
R Sessions and Functions

Department of Computer Science and Engineering,


BVCOE New Delhi
5 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
R Sessions and Functions

Department of Computer Science and Engineering,


BVCOE New Delhi
6 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
R Sessions and Functions

Department of Computer Science and Engineering,


BVCOE New Delhi
7 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
R Sessions and Functions

Department of Computer Science and Engineering,


BVCOE New Delhi
8 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
BASIC MATH

⚫ Basic Math:- R is a powerful tool for all manner calculations, data manipulation
and scientific computations. R can certainly be used to do basic math.
⚫ Examples:- > 1+1
⚫ > 1+2+3
⚫ > 3*7*2
⚫ R follows the basic order of operations: Parenthesis, Exponents, Multiplication,
Division, Addition and Subtraction (PEMDAS). This means the operations
inside parenthesis take priority over other operations. Next on the priority list is
exponentiation. After that multiplication and division are performed, followed
by addition and subtraction.
⚫ Example:- > 4 * 6 + 5
⚫ > (4 * 6) + 5
⚫ > 4 * (6 + 5)

Department of Computer Science and Engineering,


BVCOE New Delhi
9 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
VARIABLES
Variables:- Variables are integral part of any programming language. R does not require
variable types to be declared. A variable can take on any available datatype.It can hold any R
object such as a function, the result of an analysis or a plot. A single variable, at one point
hold a number, then later hold a character and then later a number again. Variable
Assignment:- There a number of waysto assign a value to a variable, it does not depend on
the type of value being assigned. There is no need to declare your variable first:
Example:-
x <- 6 # assignment operator: a less-than character ( <) and a hypen (-) with no space. >x
> y = 3 # assignment operator = is used. > y
> z <<- 9 # assignment to a global variable rather than a local variable. >z
> 5 -> fun #A rightward assignment operator (->) can be used anywhere. >fun
> a <- b <- 7 # Multiple values can be assigned simultaneously. >a, >b
> assign("k",12) # assign function can be used.>k

Department of Computer Science and Engineering,


BVCOE New Delhi
10 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
VARIABLES
Removing Variables:- rm() function is used to remove variables. This frees up memory so that R can
store more objects,althought it does not necessarily free up memory for the operating system.
There is no “undo”; once the variable is removed.
Variable names are case sensitive.
x <- 2*pi > x
> rm(x) # x variable is removed > x
Error: object 'x' not found
> rm(x,a,y) # removing multiple variables > a
Error: object 'a' not found > x
Error: object 'x' not found > y
Error: object 'y' not found
> marks <-89 # Case sensitive
> mArks
Error: object 'mArks' not found
Modifying existing variable: Rename the existing variable by using rename() function.
For examples, mydata<- rename(mydata, c(oldname="newname"))
Department of Computer Science and Engineering,
BVCOE New Delhi
11 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
VARIABLES
Variable (Object) Names: Certain variable names are reserved for particular purposes. Some
reserved symbols are: c q t C D F I T
### meaning of c q t C D F I T
? ## to see help document
?c ## c means Combine Values into a Vector or List ?q ## q means Terminate an R Session
?t ## t means Matrix Transpose
?C ## C means sets contrast for a factor
?D ## D means Symbolic and Algorithmic Derivatives of Simple Expressions
?F ## F means logical vector Character strings >F ##[1] FALSE
?I ##Inhibit Interpretation/Conversion of Objects c("T", "TRUE", "True", "true") are true,
c("F", "FALSE", "False", "false") as false, and all others as NA.

Department of Computer Science and Engineering,


BVCOE New Delhi
12 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
DATA TYPES

Department of Computer Science and Engineering,


BVCOE New Delhi
13 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
DATA TYPES

Department of Computer Science and Engineering,


BVCOE New Delhi
14 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
DATA TYPES

Department of Computer Science and Engineering,


BVCOE New Delhi
15 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
DATA TYPES

Department of Computer Science and Engineering,


BVCOE New Delhi
16 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
VECTORS
Vector:- Vectors must be homogeneous i.e, the type of data in a given vector must all be the same. Vectors
are one-dimensional arrays that can hold numeric data, character data, or logical data. The combine function
c() is used to form the vector. Here are examples of each type of vector:
⚫ > a <- c(1, 2, 5, 3, 6, -2, 4)
⚫ >a
⚫ [1] 1 2 5 3 6 -2 4
⚫ > b <- c("one", "two", "three")
⚫ >b
⚫ [1] "one" "two" "three"
⚫ > c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
⚫ >c
⚫ [1] TRUE TRUE TRUE FALSE TRUE FALSE
⚫ Here, a is numeric vector, b is a character vector, and c is a logical vector. Note that the data in a vector
⚫ must only be one type or mode (numeric, character, or logical). You can‟t mix modes in the same
⚫ vector.
⚫ Following are some other possibilities to create vectors
⚫ > x <- 1:10
⚫ >x
Department of Computer Science and Engineering,
BVCOE New Delhi
17 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
VECTORS
⚫ [1] 1 2 3 4 5 6 7 8 9 10
⚫ > y <- seq(10)
⚫ #Create a sequence
⚫ >y
⚫ [1] 1 2 3 4 5 6 7 8 9 10
⚫ > z <- rep(1,10) #Create a repetitive pattern
⚫ >z
⚫ [1] 1 1 1 1 1 1 1 1 1 1
⚫ NOTE :- Scalars are one-element vectors. Examples include f <- 3, g <- "US" and h <- TRUE. They‟re
⚫ used to hold constants.
⚫ ∙ Elements of a vector are refered using a numeric vector of positions within brackets. For
⚫ example, a[c(2, 4)] refers to the 2nd and 4th element of vector a. Here are additional
⚫ examples:
⚫ > a <- c(1, 2, 5, 3, 6, -2, 4)
⚫ > a[3]
⚫ [1] 5
⚫ > a[c(1, 3, 5)]
⚫ [1] 1 5 6
⚫ > a[2:6]
⚫ [1] 2 5 3 6 -2
⚫ ∙ The colon operator used in the last statement is used to generate a sequence of numbers. For
⚫ example, a <- c(2:6) is equivalent to a <- c(2, 3, 4, 5, 6).
⚫ ∙ If the argumentsto c(...) are themselves vectors, it flattensthem and combinesthem into
⚫ one single vector:
Department of Computer Science and Engineering,
BVCOE New Delhi
18 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
VECTORS
⚫ > v1 <- c(1,2,3)
⚫ > v2 <- c(4,5,6)
⚫ > c(v1,v2)
⚫ [1] 1 2 3 4 5 6
⚫ ∙ Vectors cannot contain a mix of data types, such as numbers and strings. If you create a vector
⚫ from mixed elements, Rwill try to accommodate you by converting one of them:
⚫ > v1 <- c(1,2,3)
⚫ > v3 <- c("A","B","C")
⚫ > c(v1,v3)
⚫ [1] "1" "2" "3" "A" "B" "C"
⚫ Here, the user tried to create a vector from both numbers and strings. R converted all the
⚫ numbers to strings before creating the vector, thereby making the data elements compatible.
⚫ ∙ Technically speaking, two data elements can coexist in a vector only if they have the same
⚫ mode. The modes of 3.1415 and "foo" are numeric and character, respectively:
⚫ > mode(3.1415)
⚫ [1] "numeric"
⚫ > mode("foo")
⚫ [1] "character"
⚫ Those modes are incompatible. To make a vector from them, R converts 3.1415 to character
⚫ modeDepartment
so it will be
ofcompatible with "foo":
Computer Science and Engineering,
BVCOE New Delhi
19 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
VECTORS
⚫ > c(3.1415, "foo")
⚫ [1] "3.1415" "foo"
⚫ > mode(c(3.1415, "foo"))
⚫ [1] "character"
⚫ ∙ Length of the vector can be obtained by length() function.
⚫ > x <- c(1,2,4)
⚫ > length(x)
⚫ [1] 3
⚫ Vector Arithmetic:
⚫ > x <- c(1:10)
⚫ >x
⚫ [1] 1 2 3 4 5 6 7 8 9 10
⚫ > y <- 10
⚫ >x+y
Department of Computer Science and Engineering,
BVCOE New Delhi
20 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
VECTORS
⚫ [1] 11 12 13 14 15 16 17 18 19 20
⚫ >2+3*x
⚫ #Note the order ofoperations
⚫ [1] 5 8 11 14 17 20 23 26 29 32
⚫ > (2 + 3) * x
⚫ #See the difference
⚫ [1] 5 10 15 20 25 30 35 40 45 50
⚫ > sqrt(x)
⚫ #Square roots
⚫ [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427
⚫ [9] 3.000000 3.162278
⚫ > x %% 4
⚫ #This is the integer divide (modulo) operation
⚫ [1] 1 2 3 0 1 2 3 0 1 2
⚫ > y <- 3 + 2i
⚫ #R does complex numbers
⚫ >x*y
⚫ [1] 3+ 2i 6+ 4i 9+ 6i 12+ 8i 15 + 10i 18 + 12i 21 + 14i 24 + 16i 27 + 18i 30 + 20i
Department of Computer Science and Engineering,
BVCOE New Delhi
21 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
ADVANCED DATA
STRUCTURES
Advanced Data Structures:- R has a wide variety of objects for holding data, including
scalars, vectors, matrices, arrays, data frames, and lists. They differ in terms of the type of
data they can hold, how they’re created, their structural complexity, and the notation used to
identify and access individual elements.

Department of Computer Science and Engineering,


BVCOE New Delhi
22 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
DATA FRAMES
⚫ Data Frames:- Data frames are tabular data objects. Has both rows and columns
analogous to excel
⚫ spreadsheet. 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. It displays data along with header information.
⚫ A data frame is created with the data.frame() function : mydata <- data.frame(col1, col2,
col3,…)
⚫ where col1, col2, col3, … are column vectors of any type (such as character, numeric,or
logical).
⚫ Names for each column can be provided with the names function

Department of Computer Science and Engineering,


BVCOE New Delhi
23 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
DATA FRAMES

Department of Computer Science and Engineering,


BVCOE New Delhi
24 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
DATA FRAMES

Department of Computer Science and Engineering,


BVCOE New Delhi
25 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
DATA FRAMES

Department of Computer Science and Engineering,


BVCOE New Delhi
26 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
LISTS
⚫ Lists: A list is a R object which can contain many different types of elements inside it like
vectors,
⚫ matrices, data frames, functions and even other lists.
⚫ mylist <- list(object1, object2, …) where the objects are any of the structures seen so far.
⚫ Example:-
⚫ >list1 <- list(c(2,5,3),21.3,sin) # Create a list.
⚫ >print(list1)
⚫ # Print the list.
⚫ [[1]]
⚫ [1] 2 5 3
⚫ [[2]]
⚫ [1] 21.3
⚫ [[3]]
⚫ function (x) .Primitive("sin")
⚫ Naming the objects in a list:
⚫ mylist <- list(name1=object1,name2=object2, …) or
Department of Computer Science and Engineering,
BVCOE New Delhi
27 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
LISTS

Department of Computer Science and Engineering,


BVCOE New Delhi
28 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
MATRICES
⚫ Matrices: A matrix is a two-dimensional array where each element has the same mode
⚫ (numeric,character, or logical). Matrices are created with the matrix function . The general
format is
⚫ myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns,
⚫ byrow=logical_value, dimnames=list(char_vector_rownames, char_vector_colnames))
⚫ where vector contains the elements for the matrix, nrow and ncol specify the row and
column
⚫ dimensions, and dimnames contains optional row and column labels stored in character
vectors. The
⚫ option byrow indicates whether the matrix should be filled in by row (byrow=TRUE) or
by column
⚫ (byrow=FALSE). The default is by column. The following listing demonstrates the matrix
⚫ function.Matrix can be created in three ways
⚫ ∙ matrix(): A vector input to the matrix function.
⚫ ∙ Using rbind() and cbind() functions.
⚫ ∙ Using dim() to the existing vector
Department of Computer Science and Engineering,
BVCOE New Delhi
29 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
MATRICES

Department of Computer Science and Engineering,


BVCOE New Delhi
30 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
MATRICES

Department of Computer Science and Engineering,


BVCOE New Delhi
31 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
MATRICES

Department of Computer Science and Engineering,


BVCOE New Delhi
32 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
MATRICES

Department of Computer Science and Engineering,


BVCOE New Delhi
33 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
MATRICES

⚫ Apply functions on matrices:- apply(), which instructs R to call a user-specified function on


each of
⚫ the rows or each of the columns of a matrix.
⚫ Using the apply() Function
⚫ This is the general form of apply for matrices: apply(m,dimcode,f,fargs) where the arguments
are :
⚫ • m is the matrix.
⚫ • dimcode is the dimension, equal to 1 if the function applies to rows or 2 for columns.
⚫ • f is the function to be applied.
⚫ • fargs is an optional set of arguments to be supplied to f.
⚫ For example, here we apply the R function mean() to each column of a matrix A:
⚫ >A
⚫ [,1][,2]
⚫ [1,] 6 1
⚫ [2,] 0 -3
⚫ > apply(A,1,mean)
⚫ [1] 3.5 -1.5

Department of Computer Science and Engineering,


BVCOE New Delhi
34 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
ARRAYS
Arrays:- An array is essentially a multidimensional vector. It must all be of the same type and
individual elements are accessed in a similar fashion using brackets. The first element is the row
index, the second is the column index and the remaining elements are for outer 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 2x3 matrices each. Creating an array:
⚫ > m< - array(1:12, dim=c(2,3,2))
⚫ >m
⚫ ,,1
⚫ [,1] [,2] [,3]
⚫ [1,] 1 3 5
⚫ [2,] 2 4 6
⚫ In the above example, “my.array” is the name of the array we
⚫ have given. There are 12 units in this array mentioned as
⚫ ,,2
⚫ “1:12” and are divided in three dimensions “(2, 3, 2)”.
⚫ [,1] [,2][,3]
⚫ [1,] 7 9 11
⚫ [2,] 8Department
10 12 of Computer Science and Engineering,
BVCOE New Delhi
35 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
ARRAYS
⚫ Alternative: with existing vector and using dim() : my.vector<- 1:24
⚫ To convert my.vector vector to an array exactly like my.array simply by assigning the
dimensions, like
⚫ this: > dim(my.vector) <- c(3,4,2)
⚫ Accessing elements of the array :-
⚫ ∙ m[1, ,] # Display every first row of the matrices
⚫ [,1] [,2]
⚫ [1,] 1 7
⚫ [2,] 3 9
⚫ [3,] 5 11
⚫ ∙ m[1, ,1] # Display first row of matrix 1
⚫ [1] 1 3 5
⚫ ∙ m[ , , 1] # Display first matrix
⚫ [,1] [,2] [,3]
⚫ [1,] 1 3 5
⚫ [2,] 2 4 6
Department of Computer Science and Engineering,
BVCOE New Delhi
36 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
CLASSES
Classes:- R possesses a simple generic function mechanism which can be used for an object-oriented style of programming. Method
dispatch takes place based on the class of the first argument to the generic function.
Usage
class(x)
class(x) <-
value unclass(x)
inherits(x, what, which = FALSE)
oldClass(x)
oldClass(x) <- value
Arguments
Details
Here, we describe the so called “S3” classes (and methods). For “S4” classes (and methods), see „Formal classes‟ below. Many R objects
have a class attribute, a character vector giving the names of the classes from which the object inherits. (Functions oldClass and oldClass<-
get and set the attribute, which can also be done directly.) If the object does not have a class attribute, it has an implicit class, notably
"matrix", "array", "function" or "numeric" or the result of typeof(x) (which is similar to mode(x)), but for type "language" and mode "call",
where the following extra classes exist for the corresponding function calls: if, while, for, =, <-, (, {, call. Note that NULL objects cannot
have attributes (hence not classes) and attempting to assign a class is an error. When a generic function fun is applied to an object with class
attribute c("first", "second"), the system searches for a function called fun.first and, if it finds it, applies it to the object. If no such function
is found, a function called fun.second is tried. If no class name produces a suitable function, the function fun.default is used (if it exists). If
there is no class attribute, the implicit class is tried, then the default method.

Department of Computer Science and Engineering,


BVCOE New Delhi
37 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
CLASSES
Formal classes
An additional mechanism of formal classes, nicknamed “S4”, is available in package methods which is attached by
default. For objects which have a formal class, its name is returned by class as a character vector of length one and
method dispatch can happen on several arguments, instead of only the first. However, S3 method selection attempts to
treat objects from an S4 class as if they had the appropriate S3 class attribute, as does inherits. Therefore, S3 methods
can be defined for S4 classes. See the „Introduction‟ and „Methods_for_S3‟ help pages for basic information on S4
methods and for the relation between these and S3 methods. The replacement version of the function sets the class to the
value provided. For classes that have a formal definition, directly replacing the class this way is strongly deprecated.
The expression as(object, value) is the way to coerce an object to a particular class. The analogue of inherits for formal
classes is is. The two functions behave consistently with one exception: S4 classes can have conditional inheritance,
with an explicit test. In this case, is will test the condition, but inherits ignores all conditional superclasses. Functions
oldClass and oldClass<- behave in the same way as functions of those names in S-PLUS 5/6, but in R UseMethod
dispatches on the class as returned by class (with some interpolated classes: see the link) rather than oldClass. However,
group generics dispatch on the oldClass for efficiency, and internal generics only dispatch on objects for which is.object
is true.

Department of Computer Science and Engineering,


BVCOE New Delhi
38 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
CLASSES
In older versions of R, assigning a zero-length vector with class removed the class: it is now an error (whereas it still works for oldClass). It is clearer to
always assign NULL to remove the class.
Examples
x <- 10
class(x) # "numeric"
oldClass(x) # NULL
inherits(x, "a") #FALSE
class(x) <- c("a", "b")
inherits(x,"a") #TRUE
inherits(x, "a", TRUE) # 1
inherits(x, c("a", "b", "c"), TRUE) # 1 2 0
class( quote(pi) )
# "name"
## regular calls
class( quote(sin(pi*x)) ) # "class"
## special calls
class( quote(x <- 1) )
# "<-"
class( quote((1 < 2)) )
# "("
class( quote( if(8<3) pi ) ) # "if"
Department of Computer Science and Engineering,
BVCOE New Delhi
39 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
R PROGRAMMING STRUCTURES & CONTROL
STATEMENTS
⚫ 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

Department of Computer Science and Engineering,


BVCOE New Delhi
40 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
41 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
42 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
43 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
44 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
45 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
46 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
47 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
48 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
49 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
50 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
51 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
52 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
Department of Computer Science and Engineering,
BVCOE New Delhi
53 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
LOOPING OVER NON-VECTOR
SETS
Looping Over Non-vector Sets:- R does not directly support iteration
over nonvectorsets, but there are a couple of indirect yet easy ways to
accomplishit:
apply( ):- Applies on 2D arrays (matrices), data frames to find
aggregate functions like sum, mean, median, standard deviation.
syntax:- apply(matrix,margin,fun, ...) margin = 1 indicates row = 2
indicates col
x <- matrix(1:20,nrow = 4,ncol=5)
>x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
> apply(x,2,sum) [1] 10 26 42 58 74
Department of Computer Science and Engineering,
BVCOE New Delhi
54 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
LOOPING OVER NON-VECTOR
SETS
Use lapply( ), assuming that the iterations of the loop are
independent of each other, thus allowing them to be performed
in any order. Lapply( ) can be applies on dataframes,lists and
vectors and return a list. lapply returns a list of the same length
as X, each element of which is the result of applying FUN to the
corresponding element of X. Syntax:lapply(X, FUN, ..)

Department of Computer Science and Engineering,


BVCOE New Delhi
55 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
LOOPING OVER NON-VECTOR
SETS

Department of Computer Science and Engineering,


BVCOE New Delhi
56 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
IF-ELSE
⚫ The if-statement in Programming Language alone tells us that if a condition is
true it will execute a block of statements and if the condition is false it won’t. But
what if we want to do something else if the condition is false? Here comes the R
Programming Language else statement. We can use the else statement with the
if statement to execute a block of code when the condition is false.
⚫ Syntax of if-else statement in R Language
⚫ if (condition) {
# code to be executed if condition is TRUE
} else {
# code to be executed if condition is FALSE
}

Department of Computer Science and Engineering,


BVCOE New Delhi
57 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
ARITHMETIC AND BOOLEAN
OPERATORS

Department of Computer Science and Engineering,


BVCOE New Delhi
58 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
ARITHMETIC AND BOOLEAN
OPERATORS

Department of Computer Science and Engineering,


BVCOE New Delhi
59 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
ARITHMETIC AND BOOLEAN
OPERATORS

Department of Computer Science and Engineering,


BVCOE New Delhi
60 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
ARITHMETIC AND BOOLEAN
OPERATORS

Department of Computer Science and Engineering,


BVCOE New Delhi
61 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
ARITHMETIC AND BOOLEAN
OPERATORS

Department of Computer Science and Engineering,


BVCOE New Delhi
62 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
ARITHMETIC AND BOOLEAN
OPERATORS

Department of Computer Science and Engineering,


BVCOE New Delhi
63 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
ARITHMETIC AND BOOLEAN
OPERATORS

Department of Computer Science and Engineering,


BVCOE New Delhi
64 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
RETURN VALUES
Return Values:- Functions are generally used for computing
some value, so they need a mechanism to supply that value back
to the caller. This is called returning.
The return value of a function can be any R object. Although the
return value is often a list, it could even be another function.
You can transmit a value back to the caller by explicitly calling
return(). Without this call, the value of the last executed
statement will be returned by default.
If the last statement in the call function is a for( ) statement,
which returns the value NULL.

Department of Computer Science and Engineering,


BVCOE New Delhi
65 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
FUNCTIONS ARE OBJECTS

Department of Computer Science and Engineering,


BVCOE New Delhi
66 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
DEFAULT VALUES FOR
ARGUMENT

⚫ Default Arguments:- R also makes frequent use of default arguments. Consider a


function definition like this:
⚫ > g <- function(x,y=2,z=T) { ... }
⚫ Here y will be initialized to 2 if the programmer does not specify y in the call.
Similarly, z will have the default value TRUE. Now consider this call:
⚫ > g(12,z=FALSE)
⚫ Here, the value 12 is the actual argument for x, and we accept the default value of 2 for
y, but we override the default for z, setting its value to FALSE.
⚫ Default Values for Arguments:-
⚫ > my_matrix<- matrix (1:12,4,3,byrow= TRUE) The argument byrow= TRUE tells R
that the matrix should be filled in row wise. In this example the default argument is
byrow = FALSE, the matrix is filled in column wise.
⚫ Lazy Evaluation of Function:- Arguments to functions are evaluated lazily, which
means so they are evaluated only when needed by the function body. # Create a function
with arguments. new.function <- function(a, b) { print(a^2) print(a) print(b) } # Evaluate
the function without supplying one of the arguments. new.function(6) When we execute
the above code, it producesthe following result − [1] 36 [1] 6 Error in print(b) :
argument "b" is missing, with no default

Department of Computer Science and Engineering,


BVCOE New Delhi
67 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
RECURSION
Recursion:-Recursion is a programming technique in which, a
function calls itself repeatedly for some input.

Department of Computer Science and Engineering,


BVCOE New Delhi
68 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula
THANKS….

Department of Computer Science and Engineering,


BVCOE New Delhi
69 Subject: Data Science Using R , Instructor: Ms 9/20/2024
Rachna Narula

You might also like