0% found this document useful (0 votes)
15 views15 pages

Week 1-R Programming Notes

Uploaded by

kisuha4928
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
15 views15 pages

Week 1-R Programming Notes

Uploaded by

kisuha4928
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

Lecture Notes on R Programming

It is recommended to visit the following website to have hands-on practice while studying these
lecture notes.

https://www.mycompiler.io/online-r-compiler
(This is the link for R Program Coding).
You will see the following interface after clicking on the above link.

Module 1
Week 1
1.1 What is the R console?
It is the place where commands written in the R language can be typed and executed.
1.2 R for Basic Math:
All common arithmetic operations and mathematical functionality are ready to use at the console
prompt. You can perform addition, subtraction, multiplication, and division with the symbols +, -,
*, and /, respectively. You can create exponents (also referred to as powers or indices) using ^,
and you control the order of the calculations in a single command using parentheses, ().

1.2.1 Arithmetic Operations:


In R, standard mathematical rules apply throughout, and follow the usual left-to-right order of
operations: parentheses, exponents, multiplication, division, addition, subtraction (PEMDAS).
Here’s an example in the R console:
Here the code to be written in R console is shown in Blue color and the output is shown in
Green color

R> 2+3
[1] 5
R> 14/6
[1] 2.333333
R> 14/6+5
[1] 7.333333
R> 14/(6+5)
[1] 1.272727
Note: Here ‘R>’ is called R prompt. [1] indicates that it is the first among many outputs.

1.2.2 Exponent:

R> 3^2
[1] 9
R> 2^3
[1] 8

1.2.3 sqrt function in R:

You can find the square root of any non-negative number with the sqrt function. You simply
provide the desired number to x as shown below:

R> sqrt(x=9)
[1] 3
R> sqrt(x=5.311)
[1] 2.304561

1.2.4 Exercise:

When using R, you’ll often find that you need to translate a complicated arithmetic formula into
code for evaluation (for example, when replicating a calculation from a textbook or research
paper). The next examples provide a mathematically expressed calculation, followed by its
execution in R
2 3×60
1. 10 + 8
− 3 Ans- 119.5
3
5 ×(6−2)
2. 61−3+4
Ans- 8.064516
2.25−0.25
2+1 2
3. 2 − 4 + 64 Ans- 16777220
0.44×(1−0.44) 0.5
4. ( 34
) Ans- 0.08512966

1.3 Assigning Variables and Objects:


So far, R has simply displayed the results of the example calculations by printing them to the
console. If you want to save the results and perform further operations, you need to be able to
assign the results of a given computation to an object in the current workspace. Put simply, this
amounts to storing some item or result under a given name so it can be accessed later, without
having to write out that calculation again. Here,we will use the terms assign and store
interchangeably. Note that some programming books refer to a stored object as a variable because
of the ability to easily overwrite that object and change it to something different, meaning that
what it represents can vary throughout a session. However, we will use the term object throughout
these notes.
You can specify an assignment in R in two ways: using arrow notation (<-) and using a single
equal sign (=). Both methods are shown here:

R> x <- 5
R> x
[1] 5
R> x = x + 1 # this overwrites the previous value of x by 1
R> x
[1] 6
R> mynumber = 45.2
R> y <- mynumber*x
R> y
[1] 271.2
R> ls()
[1] "mynumber" "x" "y"

As you can see from these examples, R will display the value assigned to an object when you
type the name of the object into the console. When you use the object in subsequent operations, R
will substitute the value you assigned to it. Finally, if you use the ls command to examine the
contents of the current workspace, it will reveal the names of the objects in alphabetical order
(along with any other previously created items). Although = and <- do the same thing, it is wise
(for the neatness of code if nothing else) to be consistent. Many users choose to stick with the <-,
however, because of the potential for confusion in using the = (for example, we clearly didn’t
mean that x is mathematically equal to x + 1 earlier). In these notes, we will do the same and
reserve = for setting function arguments, which begins. So far you’ve used only numeric values,
but note that the procedure for assignment is universal for all types and classes of objects, which
you’ll examine in the coming chapters. Objects can be named almost anything as long as the
name begins with a letter (in other words, not a number), avoids symbols (though underscores
and periods are fine), and avoids the handful of “reserved” words such as those used for defining
special values or for controlling code flow.

1.4 Vectors:
Often you’ll want to perform the same calculations or comparisons upon multiple entities, for
example if you’re rescaling measurements in a data set. You could do this type of operation one
entry at a time, though this is clearly not ideal, especially if you have a large number of items. R
provides a far more efficient solution to this problem with vectors. For the moment, to keep things
simple, you’ll continue to work with numeric entries only, though many of the utility functions
discussed here may also be applied to structures containing non-numeric values.

1.4.1 Creating a Vector:


The vector is the essential building block for handling multiple items in R. In a numerical sense,
you can think of a vector as a collection of observations or measurements concerning a single
variable, for example, the heights of 50 people or the number of coffees you drink daily. More
complicated data structures may consist of several vectors. The function for creating a vector is
the single letter c, with the desired entries in parentheses separated by commas.

R> myvec <- c(1,3,1,42)


R> myvec
[1] 1 3 1 42

Note: Vector entries can be calculations or previously stored items (including vectors
themselves).

R> my <- 32.1


R> myvec2 <- c(3,-3,2,3.45,1e+03,64^0.5,2+(3-1.1)/9.44,my)
R> myvec2
[1] 3.000000 -3.000000 2.000000 3.450000 1000.000000 8.000000
[7] 2.201271 32.100000

This code created a new vector assigned to the object myvec2. Some of the entries are defined as
arithmetic expressions, and it’s the result of the expression that’s stored in the vector. The last
element, my, is an existing numeric object defined as 32.1. Let’s look at another example.

R> myvec3 <- c(myvec,myvec2)


R> myvec3
[1] 1.000000 3.000000 1.000000 42.000000 3.000000 -3.000000
[7] 2.000000 3.450000 1000.000000 8.000000 2.201271 32.100000

This code creates and stores yet another vector, myvec3, which contains the entries of myvec and
myvec2 appended together in that order.

Note: The [1] shown in the output indicates that output is a vector and 1.000000 is its first
element and [7] indicates that 2.000000 is 7th element.
1.5 Sequences, Sorting, and Lengths:
Let’s create an equally spaced sequence of increasing or decreasing numeric values. This is
something you’ll need often, for example when programming loops or when plotting data points.
The easiest way to create such a sequence, with numeric values separated by intervals of 1, is to
use the colon operator.

R> 3:27
[1] 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

The example 3:27 should be read as “from 3 to 27 (by 1).” The result is a numeric vector just as if
you had listed each number manually in parentheses with c. As always, you can also provide
either a previously stored value or a (strictly parenthesized) calculation when using the colon
operator:

R> foo <- 5.3


R> bar <- foo:(-47+1.5)
R> bar
[1] 5.3 4.3 3.3 2.3 1.3 0.3 -0.7 -1.7 -2.7 -3.7 -4.7
[12] -5.7 -6.7 -7.7 -8.7 -9.7 -10.7 -11.7 -12.7 -13.7 -14.7 -15.7
[23] -16.7 -17.7 -18.7 -19.7 -20.7 -21.7 -22.7 -23.7 -24.7 -25.7 -26.7
[34] -27.7 -28.7 -29.7 -30.7 -31.7 -32.7 -33.7 -34.7 -35.7 -36.7 -37.7
[45] -38.7 -39.7 -40.7 -41.7 -42.7 -43.7 -44.7

Note: The name foo and bar is not a real name: it is a placeholder, used to represent the name
of any desired thing.

1.5.1 Sequences with seq:


You can also use the seq command, which allows for more flexible creations of sequences. This
ready-to-use function takes in a from value, a to value, and a by value, and it returns the
corresponding sequence as a numeric vector.

R> seq(from=3,to=27,by=3)
[1] 3 6 9 12 15 18 21 24 27

This gives you a sequence with intervals of 3 rather than 1.


Note:These kinds of sequences will always start at the front number but will not always include
the last number, depending on what you are asking R to increase (or decrease) them by. For
example, if you are increasing (or decreasing) by even numbers and your sequence ends in an odd
number, the final number won’t be included.
1.5.2 length.out value:
Instead of providing a by value, however, you can specify a length.out value to produce a vector
with that many numbers, evenly spaced between the from and to values.
R> seq(from=3,to=27,length.out=40)
[1] 3.000000 3.615385 4.230769 4.846154 5.461538 6.076923 6.692308
[8] 7.307692 7.923077 8.538462 9.153846 9.769231 10.384615 11.000000
[15] 11.615385 12.230769 12.846154 13.461538 14.076923 14.692308 15.307692 22
[22] 15.923077 16.538462 17.153846 17.769231 18.384615 19.000000 19.615385
[29] 20.230769 20.846154 21.461538 22.076923 22.692308 23.307692 23.923077
[36] 24.538462 25.153846 25.769231 26.384615 27.000000

By setting length.out to 40, you make the program print exactly 40 evenly spaced numbers from
3 to 27.

1.5.3 Sorting with sort:


Sorting a vector in increasing or decreasing order of its elements is another simple operation
that crops up in everyday tasks. The conveniently named sort function does just that.

R> sort(x=c(2.5,-1,-10,3.44),decreasing=FALSE)
[1] -10.00 -1.00 2.50 3.44
R> sort(x=c(2.5,-1,-10,3.44),decreasing=TRUE)
[1] 3.44 2.50 -1.00 -10.00
R> foo <- seq(from=4.3,to=5.5,length.out=8)
R> foo
[1] 4.300000 4.471429 4.642857 4.814286 4.985714 5.157143 5.328571 5.500000
R> bar <- sort(x=foo,decreasing=TRUE)
R> bar
[1] 5.500000 5.328571 5.157143 4.985714 4.814286 4.642857 4.471429 4.300000
R> sort(x=c(foo,bar),decreasing=FALSE)
[1] 4.300000 4.300000 4.471429 4.471429 4.642857 4.642857 4.814286 4.814286
[9] 4.985714 4.985714 5.157143 5.157143 5.328571 5.328571 5.500000 5.500000

The sort function is pretty straightforward. You supply a vector to the function as the argument x,
and a second argument, decreasing, indicates the order in which you want to sort. This argument
takes a type of value you have not yet met: one of the all-important logical values. A logical
value can be only one of two specific, case-sensitive values: TRUE or FALSE. Generally
speaking, logicals are used to indicate the satisfaction or failure of a certain condition, and they
form an integral part of all programming languages. You’ll investigate logical values in R in
greater detail. For now, in regards to sort, you set decreasing=FALSE to sort from smallest to
largest, and decreasing=TRUE sorts from largest to smallest.
1.5.4 Vector Length with length:
The length function, which determines how many entries exist in a vector given as the argument
x.

R> length(x=c(3,2,8,1))
[1] 4
R> length(x=5:13)
[1] 9
R> foo <- 4
R> bar <- c(3,8.3,rep(x=32,times=foo),seq(from=-2,to=1,length.out=foo+1))
R> length(x=bar)
[1] 11

1.6 Defining a Matrix:


The matrix is an important mathematical construct, and it’s essential to many statistical methods,
particularly when you’re dealing with multivariate observations (see Chapter 13). You will
typically describe a matrix A as an m × n matrix; that is, A will have exactly m rows and n
columns. A will have a total of mn entries, with each entry a(i, j) having a unique position given
by its specific row (i = 1,2,. . . ,m) and column ( j = 1,2,. . . ,n).
To create a matrix in R, use the aptly named matrix command.

R> A <- matrix(data=c(-3,2,893,0.17),nrow=2,ncol=2)


R> A
[,1] [,2]
[1,] -3 893.00
[2,] 2 0.17

Note: The entries of the matrix are provided as a vector to the data argument, and you must make
sure that the length of this vector matches exactly with the number of desired rows (nrow) and
columns (ncol). You can elect not to supply nrow and ncol when calling matrix, in which case R’s
default behavior is to return a single-column matrix of the entries in data. For example,
matrix(data=c(-3,2,893,0.17)) is identical to matrix(data=c(-3,2,893,0.17),nrow=4,ncol=1).

1.6.1 cbind( ) and rbind( ) functions:


Matrices can be created by column-binding or row-binding with the cbind( ) and rbind( )
functions.
R>x <- 1:3
R>y <- 10:12
R>cbind(x, y)
xy
[1,] 1 10
[2,] 2 11
[3,] 3 12

R>rbind(x, y)
[,1] [,2] [,3]
x123
y 10 11 12

1.6.2 Matrix Operations and Algebra:


You can think of matrices in R from two perspectives. First, you can use these structures purely
as a computational tool in programming to store and operate on results. Alternatively, you can use
matrices for their mathematical properties in relevant calculations. This distinction is important
when handling these data structures in the software because the mathematical behavior of
matrices does not always match the way they are generically handled by the language. Here we
will briefly describe some special matrices, as well as some of the most common mathematical
operations involving matrices, and will demonstrate the corresponding functionality in R.

1.6.3 Matrix Transpose:


𝑇
For any m × n matrix A, its transpose, 𝐴 , is the n × m matrix obtained by writing its columns as
rows or, equivalently, its rows as columns. Here’s an example:

In R, the transpose of a matrix is found with the function t.In R, the transpose of a matrix is
found with the function t.

R> A <- rbind(c(2,5,2),c(6,1,4))


R> A
[,1] [,2] [,3]
[1,] 2 5 2
[2,] 6 1 4
R> t(A)
[,1] [,2]
[1,] 2 6
[2,] 5 1
[3,] 2 4
Note: Taking the “transpose of the transpose” of A will recover the original matrix. R>
t(t(A))
[,1] [,2] [,3]
[1,] 2 5 2
[2,] 6 1 4

1.6.4 Identity Matrix:


The identity matrix written as Im is a square m × m matrix with ones on the diagonal and zeros
elsewhere. Here’s an example:

You can create an identity matrix of any dimension using the standard matrix function, but there
is a quicker approach using diag.

R> A <- diag(x=3)


R> A
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1

Here you see diag can be used to easily produce an identity matrix. To clarify, the behavior of
diag depends on what you supply to it as its argument x. If, as earlier, x is a matrix, diag will
retrieve the diagonal elements of the matrix. If x is a single positive integer, as is the case here,
then diag will produce the identity matrix of the corresponding dimension.

1.6.5 Scalar Multiple of a Matrix:


Multiplication of any matrix A by a scalar (single, univariate) value results in a matrix in which
every individual element is multiplied by a. Here’s an example:

The notion of every element in a matrix being multiplied by some constant amount fits right into
R’s element-wise behavior. Scalar multiplication of a matrix is carried out using the standard
arithmetic * operator.
R> A <- rbind(c(2,5,2),c(6,1,4))
R> a <- -2.5
R> a*A
[,1] [,2] [,3]
[1,] -5 -12.5 -5
[2,] -15 -2.5 -10
1.6.6 Matrix Addition and Subtraction:
For any two matrices A and B of equal size, addition or subtraction is performed in an
element-wise fashion, where corresponding elements are added or subtracted from one another,
depending on the operation. Here’s an example:

Again, the standard operational mode of R is to proceed in an elementwise fashion, which means
you can add or subtract any two equally sized matrices with the standard + and - symbols.

R> A <- cbind(c(2,5,2),c(6,1,4))


R> A
[,1] [,2]
[1,] 2 6
[2,] 5 1
[3,] 2 4
R> B <- cbind(c(-2,3,6),c(8.1,8.2,-9.8))
R> B
[,1] [,2]
[1,] -2 8.1
[2,] 3 8.2
[3,] 6 -9.8
R> A-B
[,1] [,2]
[1,] 4 -2.1
[2,] 2 -7.2
[3,] -4 13.8

1.6.7 Matrix Multiplication:


Suppose you have two matrices A and B of size m × n and p × q, respectively. You can perform
matrix multiplication to compute their product A · B if and only if n = p. The resulting matrix
will have the size m × q. The elements of the product are computed in a row-by-column fashion,
where the value at position (AB)i, j is computed by element-wise multiplication of the entries in
row i of A by the entries in column j of B, summing the result. Here’s an example:
Note that, in general,
multiplication of appropriately sized matrices (denoted, say, with C and D) is not commutative;
that is, CD , DC. Unlike addition, subtraction, and scalar multiplication, matrix multiplication is
not a simple element-wise calculation, and the standard * operator cannot be used for this
operation. Instead, you must use R’s matrix product operator, written with percent symbols as
%*%. Before you try this operator, let’s first store the two matrices in the previous example and
check to make sure the number of columns in the first matrix matches the number of rows in the
second matrix using dim.

R> A <- rbind(c(2,5,2),c(6,1,4))


R> dim(A)
[1] 2 3
R> B <- cbind(c(3,-1,1),c(-3,1,5))
R> dim(B)
[1] 3 2

This confirms the two matrices are compatible to be multiplied by one another, so you can
proceed.

R> A%*%B
[,1] [,2]
[1,] 3 9
[2,] 21 3

You can show that matrix multiplication is noncommutative using the same two matrices.
Switching the order of multiplication gives you an entirely different result.
R> B%*%A
[,1] [,2] [,3]
[1,] -12 12 -6
[2,] 4 -4 2
[3,] 32 10 22
1.6.8 Matrix Inversion:

R> A <- matrix(data=c(3,4,1,2),nrow=2,ncol=2)


R> A
[,1] [,2]
[1,] 3 1
[2,] 4 2
R> solve(A)
[,1] [,2]
[1,] 1 -0.5
[2,] -2 1.5

You can also verify that the product of these two matrices (using matrix multiplication rules)
results in the 2 × 2 identity matrix.

R> A%*%solve(A)
[,1] [,2]
[1,] 1 0
[2,] 0 1
Exercise

1.1 Vimal Industries has issued 60,000 shares of par value Rs. 10 each. The company
declared a total dividend of Rs. 72,000. Find the rate of dividend paid by the company.
Calculate using R Programming.

1.2 Create a vector with following numbers 3, 67.0.98,123,67.89,-0.34 and store it as a new
object in R. Sort this vector in ascending order and measure its length. Also create a
sequence of exactly 42 evenly spaced numbers from 5 to 30.

1.3

𝑇
Calculate A𝐵
*****

You might also like