1 of 69

DATA SCIENCE USING R

VIII SEMESTER

DS-427T

UNIT-2

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

1

9/20/2024

2 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

2

9/20/2024

3 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

3

9/20/2024

4 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

4

9/20/2024

5 of 69

R Sessions and Functions

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

5

9/20/2024

6 of 69

R Sessions and Functions

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

6

9/20/2024

7 of 69

R Sessions and Functions

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

7

9/20/2024

8 of 69

R Sessions and Functions

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

8

9/20/2024

9 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

9

9/20/2024

10 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

10

9/20/2024

11 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

11

9/20/2024

12 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

12

9/20/2024

13 of 69

DATA TYPES

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

13

9/20/2024

14 of 69

DATA TYPES

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

14

9/20/2024

15 of 69

DATA TYPES

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

15

9/20/2024

16 of 69

DATA TYPES

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

16

9/20/2024

17 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

17

9/20/2024

18 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

18

9/20/2024

19 of 69

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
  • mode so it will be compatible with "foo":

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

19

9/20/2024

20 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

20

9/20/2024

21 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

21

9/20/2024

22 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

22

9/20/2024

23 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

23

9/20/2024

24 of 69

DATA FRAMES

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

24

9/20/2024

25 of 69

DATA FRAMES

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

25

9/20/2024

26 of 69

DATA FRAMES

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

26

9/20/2024

27 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

27

9/20/2024

28 of 69

LISTS

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

28

9/20/2024

29 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

29

9/20/2024

30 of 69

MATRICES

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

30

9/20/2024

31 of 69

MATRICES

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

31

9/20/2024

32 of 69

MATRICES

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

32

9/20/2024

33 of 69

MATRICES

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

33

9/20/2024

34 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

34

9/20/2024

35 of 69

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,] 8 10 12

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

35

9/20/2024

36 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

36

9/20/2024

37 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

37

9/20/2024

38 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

38

9/20/2024

39 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

39

9/20/2024

40 of 69

R PROGRAMMING STRUCTURES & CONTROL STATEMENTS

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

40

9/20/2024

41 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

41

9/20/2024

42 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

42

9/20/2024

43 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

43

9/20/2024

44 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

44

9/20/2024

45 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

45

9/20/2024

46 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

46

9/20/2024

47 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

47

9/20/2024

48 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

48

9/20/2024

49 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

49

9/20/2024

50 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

50

9/20/2024

51 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

51

9/20/2024

52 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

52

9/20/2024

53 of 69

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

53

9/20/2024

54 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

54

9/20/2024

55 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

55

9/20/2024

56 of 69

LOOPING OVER NON-VECTOR SETS

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

56

9/20/2024

57 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

57

9/20/2024

58 of 69

ARITHMETIC AND BOOLEAN OPERATORS

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

58

9/20/2024

59 of 69

ARITHMETIC AND BOOLEAN OPERATORS

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

59

9/20/2024

60 of 69

ARITHMETIC AND BOOLEAN OPERATORS

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

60

9/20/2024

61 of 69

ARITHMETIC AND BOOLEAN OPERATORS

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

61

9/20/2024

62 of 69

ARITHMETIC AND BOOLEAN OPERATORS

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

62

9/20/2024

63 of 69

ARITHMETIC AND BOOLEAN OPERATORS

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

63

9/20/2024

64 of 69

ARITHMETIC AND BOOLEAN OPERATORS

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

64

9/20/2024

65 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

65

9/20/2024

66 of 69

FUNCTIONS ARE OBJECTS

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

66

9/20/2024

67 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

67

9/20/2024

68 of 69

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 Subject: Data Science Using R , Instructor: Ms Rachna Narula

68

9/20/2024

69 of 69

THANKS….

Department of Computer Science and Engineering, BVCOE New Delhi Subject: Data Science Using R , Instructor: Ms Rachna Narula

69

9/20/2024