Nirula R Programming Lab Manual (1)
Nirula R Programming Lab Manual (1)
LAB MANUAL
MASTER OF BUSINESS
ADMINISTRATION
R PROGRAMMING
Objectives:
This course provides the knowledge to Install and use R for simple programming
tasks, extended R libraries and packages. Which helps to Develop R
Programs using Looping Constructs and R mathematical functions that can
be used for data exploration in R.
Course Outcomes:
1
R PROGRAMMING
TEXT BOOKS:
T1: The Art of R Programming, Norman Matloff, Cengage Learning
T2: R for Everyone, Lander, Pearson
REFERENCE BOOKS:
R1: R Cookbook, PaulTeetor, Oreilly.
R2: R in Action, Rob Kabacoff, Manning.
2
R PROGRAMMING
INDEX
S.No Program Description Page No.
1. History of R 4-5
3
R PROGRAMMING
History of R
5
R PROGRAMMING
Exercise -II
To Install R and R Packages
To Install RStudio
To Install R Packages
6
.libPaths() # get library location library()
# see all packages installed search() #
see packages currently loaded
Adding R Packages
You can expand the types of analyses you do be adding other packages. A
complete list of contribu ted packages is available from CRAN.
The Workspace
The workspace is your current R working environment and includes any
user-defined objects (vectors, matrices, data frames, lists, functions). At the
end of an R session, the user can save an image of the current workspace
that is automatically reloaded the next time R is started. Commands are
entered interactively at the R user prompt. Up and down arrow keys
scroll through your command history.
<- 20
Function Description
ceiling(x) ceiling(3.475) is 4
floor(x) floor(3.475) is 3
trunc(x) trunc(5.99) is 5
exp(x) e^x
Creating Vector
Vectors are generally created using the c() function.
Since, a vector must have elements of the same type, this function will try
and coerce elements to the same type, if they are different.
Coercion is from lower to higher types from logical to integer to double to
character.
> typeof(x)
[1] "double"
> length(x)
[1] 5
>x
> typeof(x)
[1] "character"
[1] 1 2 3 4 5 6 7
[1] 2 1 0 -1 -2
More complex sequences can be created using the seq() function, like
defining number of points in an interval, or the step size.
[1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0
VECTORS EXERCISE - I
1. Consider two vectors, x, y
x=c(4,6,5,7,10,9,4,15)
y=c(0,10,1,8,2,3,4,1) What is the value of: x*y and x+y
3. If x=c(1:12)
What is the value of: dim(x) What
is the value of: length(x) Sol:
> x<-c(1:12)
> dim(x)
NULL
> length(x)
[1] 12
VECTORS EXERCISE - II
1. The numbers below are the first ten days of rainfall amounts in 1996.
Read them in to a vector using the c() function 0.1, 0.6, 33.8, 1.9, 9.6,
4.3, 33.7, 0.3, 0.0, 0.1
Sol:
> rainfall<-c(0.1, 0.6, 33.8, 1.9, 9.6, 4.3, 33.7, 0.3, 0.0, 0.1)
> rainfall
[1] 0.1 0.6 33.8 1.9 9.6 4.3 33.7 0.3 0.0 0.1
2. Inspect Table and answer the following questions:
I. What was the mean rainfall, how about the standard deviation?
Sol:
rainfall
[1] 0.1 0.6 33.8 1.9 9.6 4.3 33.7 0.3 0.0 0.1
> mean(rainfall) [1] 8.44
> sd(rainfall)
[1] 13.66473
II. Calculate the cumulative rainfall (’running total’) over these ten days.
Confirm that the last value of the vector that this
produces is equal to the total sum of the rainfall.
Sol:
> rainfall
[1] 0.1 0.6 33.8 1.9 9.6 4.3 33.7 0.3 0.0 0.1
> cumsum(rainfall)
[1] 0.1 0.7 34.5 36.4 46.0 50.3 84.0 84.3 84.3 84.4
> sum(rainfall)==rainfall[10] [1] FALSE
III. Which day saw the highest rainfall? Hint
which.max()
Sol:
> rainfall
[1] 0.1 0.6 33.8 1.9 9.6 4.3 33.7 0.3 0.0 0.1
> max(rainfall) [1] 33.8
3. Compute the problem sum ((x - mean(x)) ^2).
Sol:
> x<-c(1:10)
> sum ((x - mean(x)) ^2) [1]
82.5
4. The weights of five people before and after a diet programme are
given in the table.
Read the `before' and `after' values into two different vectors called
before and after. Use R to evaluate the amount of weight lost for each
participant. What is the average amount of weight lost?
Sol:
> before
[1] 78 72 78 79 105
> after
[1] 67 65 79 70 93
> weightlost<-before-after
> weightlost
[1] 11 7 -1 9 12
> mean(weightlost) [1]
7.6
Exercise – VII MATRICES
EXERCISE - I
Creating Matrices
To create matrices we will use the matrix() function. The matrix() function
takes the following arguments:
• data an R object (this could be a vector).
• nrow the desired number of rows.
• ncol the desired number of columns.
• byrow a logical statement to populate the matrix by either row or by
column.
Creation of matrix
a) matrix1 <- matrix ( data = 1, nrow = 3, ncol = 3) Sol:
> matrix1 <- matrix ( data = 1, nrow = 3, ncol = 3)
> matrix1
[,1] [,2] [,3] [1,] 1
1 1 [2,]
1 1 1
[3,] 1 1 1
b) vector8 <- 1:12 matrix3 <- matrix ( data = Vector8 , nrow =
4) Sol:
> vector8 <- c(1:12)
> vector8
[1] 1 2 3 4 5 6 7 8 9 10 11 12
> matrix3 <- matrix ( data = vector8 , nrow = 4)
> matrix3
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
c) v1<- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3)
Sol:
> v1<- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3)
> v1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
d) v2<- matrix(1:8, ncol = 2)
Sol:
> v2<- matrix(1:8, ncol = 2)
> v2
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
e) matrix1 = matrix(1:9, nrow = 3) matrix1 + 2
Sol:
> matrix1 = matrix(1:9, nrow = 3)
> matrix1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> matrix1+2
[,1] [,2] [,3]
[1,] 3 6 9
[2,] 4 7 10
[3,] 5 8 11
Manipulation of Matrix
f) matrix1
Sol:
> matrix1
[,1] [,2] [,3] [1,] 1
4 7 [2,]
2 5 8
[3,] 3 6 9
g) matrix1[1, 3] Sol:
> matrix1[1, 3] [1] 7
h) matrix1[ 2, ] Sol:
> matrix1[ 2, ] [1]
258
i) matrix1[,-2] Sol:
> matrix1[,-2] [,1]
[,2]
[1,] 1 7
[2,] 2 8
[3,] 3 9
j) matrix1[1, 1] = 15
Sol:
> matrix1[1, 1] = 15
> matrix1
[,1] [,2] [,3] [1,]
15 4 7
[2,] 2 5 8
[3,] 3 6 9
k) matrix1[ ,2 ] = 1
Sol:
> matrix1
[,1] [,2] [,3] [1,]
15 1 7
[2,] 2 1 8
[3,] 3 1 9
l) matrix1[ ,2:3 ] = 2
Sol:
> matrix1[ ,2:3 ] = 2
> matrix1
[,1] [,2] [,3]
[1,] 15 2 2
[2,] 2 2 2
[3,] 3 2 2
Exercise – VIII MATRICES
EXERCISE - II
Mathematical Operations
R can do matrix arithmetic. Below is a list of some basic operations we can
do.
+ - * / standard scalar or by element operations
%*% matrix multiplication
t() transpose
solve() inverse
det() determinant
chol() cholesky decomposition
eigen() eigenvalues and eigenvectors
crossprod() cross product.
h) Now remake the above matrix with 100 columns, and 10 rows. Then
calculate the column means (using, of course, colMeans).
Sol:
>m <- matrix(runif(1000), ncol=100,nrow=10)
> m1<-colMeans(m)
> m1
Exercise – IX
Q.1
Q.2
Scalar multiplication. Find the solution for aA where a=3 and A is the same
as in the previous question.
Q.3
Using the the diag function build a diagonal matrix of size 4 with the
following values in the diagonal 4,1,2,3.
Q.4
Find the solution for Ab, where A is the same as in the previous question
and b=c(7,4).
Q.5
Q.6
Q.7
Q.9
Q.10
Factors
Factor is a data structure used for fields that takes only predefined, finite
number of values (categorical data).
For example, a data field such as marital status may contain only values
from single, married, separated, divorced, or widowed.
In such case, we know the possible values beforehand and these
predefined, distinct values are called levels. Following is an example of
factor in R.
>x
Here, we can see that factor x has four elements and two levels. We can
check if a variable is a factor or not using class() function.
Similarly, levels of a factor can be checked using the levels() function.
"factor"
> levels(x)
Creating factor in R?
We can create a factor using the function factor(). Levels of a factor are
inferred from the data if not provided.
>x
We can see from the above example that levels may be predefined even if
not used.
Factors are closely related with vectors. In fact, factors are stored as integer
vectors. This is clearly seen from its structure.
> str(x)
> levels(factor(x))
Q.3 If z <- c("p", "a" , "g", "t", "b"), then What is the R expression will re
place the third element in z with "b".
Sol:
> z <- c("p", "a" , "g", "t", "b")
> z[3] <- "b"
>z
[1] "p" "a" "b" "t" "b"
Q.4 If z <- factor(c("p", "q", "p", "r", "q")) and levels of z are "p", "q" ,"r",
write an R expression that will change the level "p" to "w" so that z is e
qual to: "w", "q" , "w", "r" , "q".
Sol:
> z <- factor(c("p", "q", "p", "r", "q"))
> levels(z)[1] <- "w"
>z
[1] w q w r q
Levels: w q r
Sol:
> s1 <- factor(sample(letters, size=5, replace=TRUE))
> s2 <- factor(sample(letters, size=5, replace=TRUE))
> factor(c(levels(s1)[s1], levels(s2)[s2]))
[1] c e q l t v b k t c
Levels: b c e k l q t v
Sol:
> responses <- factor(c("Agree", "Agree", "Strongly Agree", "Disagree", "Agr
ee"))
> responses
[1] Agree Agree Strongly Agree Disagree Agree
Levels: Agree Disagree Strongly Agree
Sol:
> x <- factor(c("high", "low", "medium", "high", "high", "low", "medium"))
> data.frame(levels = unique(x), value = as.numeric(unique(x)))
levels value
1 high 1
2 low 2
3 medium 3
Viva Voice Questions and Answers - Cycle-I
1. What is R?
R is a programming language which is used for developing statistical
software and data analysis. It is being increasingly deployed for
machine learning applications as well.
2. How R commands are written?
By using # at the starting of the line of code like #division commands are
written.
3. What is t-tests() in R?
It is used to determine that the means of two groups are equal or not by
using t.test() function.
4. What are the disadvantages of R Programming?
The disadvantages are:-
Lack of standard GUI
Not good for big data.
Does not provide spreadsheet view of data.
5. What is the use of With () and By () function in R?
with() function applies an expression to a dataset.
#with (data, expression)
By() function applies a function t each level of a factors.
#by (data, factor list, function)
6. In R programming, how missing values are represented?
In R missing values are represented by NA which should be in capital
letters.
7. What is the use of subset() and sample() function in R?
Subset() is used to select the variables and observations and sample()
function is used to generate a random sample of the size n from a
dataset.
8. Explain what is transpose.
Transpose is used for reshaping of the data which is used for
analysis. Transpose is performed by t() function.
9. What are the advantages of R?
The advantages are:-
It is used for managing and manipulating of data.
No license restrictions
Free and open source software.
Graphical capabilities of R are good.
Runs on many Operating system and different hardware and also run
on 32 & 64 bit processors etc.
10. What is the function used for adding datasets in R?
For adding two datasets rbind() function is used but the column of two
datasets must be same.
Syntax: rbind(x1,x2……) where x1,x2: vector, matrix, data frames.
11. How you can produce co-relations and covariances?
Cor-relations is produced by cor() and covariances is produced by
cov() function.
12. What is difference between matrix and dataframes?
Dataframe can contain different type of data but matrix can contain
only similar type of data.
13. What is difference between lapply and sapply?
lapply is used to show the output in the form of list whereas sapply is
used to show the output in the form of vector or data frame
14. What is the difference between seq(4) and seq_along(4)?
Seq(4) means vector from 1 to 4 (c(1,2,3,4)) whereas seq_along(4)
means a vector of the length(4) or 1(c(1)).
15. Explain how you can start the R commander GUI.
rcmdr command is used to start the R commander GUI.
16. What is the memory limit of R?
In 32 bit system memory limit is 3Gb but most versions limited to
2Gb and in 64 bit system memory limit is 8Tb.
17. How many data structures R has?
There are 5 data structure in R i.e. vector, matrix, array which are of
homogenous type and other two are list and data frame which are
heterogeneous.
18. Explain how data is aggregated in R.
There are two methods that is collapsing data by using one or more BY
variable and other is aggregate() function in which BY variable should
be in list.
19. How many sorting algorithms are available?
There are 5 types of sorting algorithms are used which are:-
Bubble Sort
Selection Sort
Merge Sort
Quick Sort
Bucket Sort
20. How to create new variable in R programming?
For creating new variable assignment operator ‘< -’ is used
For e.g. mydata$sum <- mydata$x1 + mydata$x2
21. What are R packages?
Packages are the collections of data, R functions and compiled code in a
well-defined format and these packages are stored in library. One of the
strengths of R is the user-written function in R language.
22. What is the workspace in R?
Workspace is the current R working environment which includes any
user defined objects like vector, lists etc.
23. What is the function which is used for merging of data frames
horizontally in R?
Merge()function is used to merge two data frames
Eg. Sum<-merge(data frame1,data frame 2,by=’ID’)
24. What is the function which is used for merging of data frames
vertically in R?
rbind() function is used to merge two data frames vertically. Eg.
Sum<- rbind(data frame1,data frame 2)
25. What is the power analysis?
It is used for experimental design .It is used to determine the effect of
given sample size.
26. Which method is used for exporting the data in R?
There are many ways to export the data into another formats like
SPSS, SAS , Stata , Excel Spreadsheet.
27. Which packages are used for exporting of data?
For excel xlsReadWrite package is used and for sas,spss ,stata foreign
package is implemented.
28. How impossible values are represented in R?
In R NaN is used to represent impossible values.
29. Which command is used for storing R object into a file?
Save command is used for storing R objects into a file. Syntax:
>save(z,file=”z.Rdata”)
30. Which command is used for restoring R object from a file?
load command is used for storing R objects from a file.
Syntax: >load(”z.Rdata”)
31. What is the use of coin package in R?
Coin package is used to achieve the re randomization or permutation
based statistical tests.
Data Frames
Data frame is a two dimensional data structure in R. It is a special case of a
list which has each component of equal length.
Each component form the column and contents of the compon ent form the
rows.
Creating Data Frame in R
We can create a data frame using the data.frame() function.
For example, the above shown data frame can be created as follows.
$ SN : int 1 2
$ Age : num 21 15
Notice above that the third column, Name is of type factor, instead of a
character vector.
By default, data.frame() function converts character vector into factor.
To suppress this behavior, we can pass the argument
stringsAsFactors=FALSE.
$ Age : num 21 15
> x["Name"]
Name
1 John
2 Dora
> x$Name
> x[["Name"]]
> x[[3]]
Question 1
Create the following data frame, afterwards invert Sex for all individuals.
Answer:
Name <- c("Alex", "Lilly", "Mark", "Oliver", "Martha", "Lucas", "Caroline")
Age <- c(25, 31, 23, 52, 76, 49, 26)
Height <- c(177, 163, 190, 179, 163, 183, 164)
Weight <- c(57, 69, 83, 75, 70, 83, 53)
Sex <- as.factor(c("F", "F", "M", "M", "F", "M", "F"))
df <- data.frame (row.names = Name, Age, Height, Weight, Sex)
levels(df$Sex) <- c("M", "F")
df
Question 2
Create this data frame (make sure you import the variable working as
character and not factor).
dim(dfa)
## [1] 7 5
# or:
nrow(dfa)
## [1] 7
ncol(dfa)
## [1] 5
b) sapply(dfa, class)
## Age Height Weight Sex Working
## "numeric" "numeric" "numeric" "factor" "factor"
str(dfa) # alternative solution
Question 3
Create a simple data frame from 3 vectors. Order the entire data frame by
the first column.
# Example vectors
v <- c(45:41, 30:33)
b <- LETTERS[rep(1:3, 3)]
n <- round(rnorm(9, 65, 5))
OR
df [order(df$Age), ]
Question 4
Create a data frame from a matrix of your choice, change the row names so
every row says id_i (where i is the row number) and change the column
names to variable_i (where i is the column number). I.e., for column 1 it will
say variable_1, and for row 2 will say id_2 and so on.
Answer:
matr <- matrix(1:20, ncol = 5) # Example matrix
df <- as.data.frame(matr)
colnames(df) <- paste("variable_", 1:ncol(df))
rownames(df) <- paste("id_", 1:nrow(df))
df
Question -5
Take Q.1 into consideration answer the Following Questions
a) What are the Names of Students?
b) Find the Mean Height of Students and Weight of Students.
c) Find the Standard Deviation of Height and Weight of Students. d)
Find the number of Male and Female Students.
Question - 6
The command data.frame() is used to create a data frame, each argument
representing a column.
> books <- data.frame(author=c("Ripley", "Cox", "Snijders", "Cox"),
+ year=c(1980, 1979, 1999, 2006),
+ publisher=c("Wiley", "Chapman", "Sage", "CUP"))
> books
author year publisher
1 Ripley 1980 Wiley
2 Cox 1979 Chapman
3 Snijders 1999 Sage
4 Cox 2006 CUP
(a) Create a small data frame representing a database of films. It should
contain the fields title, director, year, country, and at least three films.
(b) Create a second data frame of the same format as above, but containing
just one new film.
(c) Merge the two data frames using rbind().
(d) Try sorting the titles using sort(): what happens?
Exercise – XII
LISTS
List is a data structure having components of mixed data types.
A vector having all elements of the same type is called atomic vector but a
vector having elements of different type is called list.
We can check if it’s a list with typeof() function and find its length using
length().
Creating a list
Here, we create a list x, of three components with data types double, logical
and integer vector respectively.
Its structure can be examined with the str() function.
> str(x)
We can create the same list without the tags as follows. In such scenario,
numeric indices are used by default.
>x
Q.1 If: p <- c(2,7,8), q <- c("A", "B", "C") and x <- list(p, q), then what is
the value of x[2]?
Sol:
p <- c(2,7,8)
q <- c("A", "B", "C")
x <- list(p, q)
x[2]
[[1]]
[1] "A" "B" "C"
Q.2 If: w <- c(2, 7, 8) v <- c("A", "B", "C") x <- list(w, v), then which R sta
tement will replace "A" in x with "K".
Sol:
w <- c(2, 7, 8)
v <- c("A", "B", "C")
x <- list(w, v) x[[2]]
[1] <- "K"
x
[[1]]
[1] 2 7 8
[[2]]
[1] "K" "B" "C"
Q.3 If a <- list ("x"=5, "y"=10, "z"=15), which R statement will give the
sum of all elements in a?
Sol:
a <- list ("x"=5, "y"=10, "z"=15)
sum(unlist(a))
[1] 30
$a
[1] 2 3 4 5 6 7 8 9 10 11
$b
[1] "Good morning"
$c
[1] "Hi"
[1] 1 3 4 5 6 7 8 9 10
Q.7 Consider y <- list("a", "b", "c"), write an R statement that will
assign new names "one", "two" and "three" to the elements of y.
Sol:
y <- list("a", "b", "c")
names(y) <- c("one", "two", "three")
y
$one
[1] "a"
$two
[1] "b"
$three
[1] "c"
Q.8 If x <- list(y=1:10, t="Hello", f="TT", r=5:20), write an R statement
that will give the length of vector r of x.
Sol:
x <- list(y=1:10, t="Hello", f="TT", r=5:20)
length(x$r)
[1] 16
Q.9 Let string <- "Grand Opening", write an R statement to split this
string into two and return the following output:
Sol:
> string <- "Grand Opening"
> a <- strsplit(string," ")
> list(a[[1]][1], a[[1]][2])
[[1]]
[1] "Grand"
[[2]]
[1] "Opening"
Q.10 Let: y <- list ("a", "b", "c") and q <- list ("A", "B", "C", "a", "b", "c").
Write an R statement that will return all elements of q that are not in
y, with the following result:
Sol:
> y <- list("a", "b", "c")
> q <- list("A", "B", "C", "a", "b", "c")
> setdiff(q, y)
[[1]] [1]
"A"
[[2]] [1]
"B"
[[3]] [1]
"C"
Exercise – XIII
Operators in R
R has many operators to carry out different mathematical and logical
operations. Operators in R can mainly be classified into the following
categories.
Type of operators in R
Arithmetic operators
R e la t i o n a l o p e r a t o r s
Logical operators
Assignment operators
R Arithmetic Operators
These operators are used to carry out mathematical operations like
addition and multiplication. Here is a list of arithmetic operators available
in R.
Arithmetic Operators in R
Operator Description
+ Addition
– Subtraction
* Multiplication
/ Division
^ Exponent
R Relational Operators
Relational operators are used to compare between values. Here is a list of
relational operators available in R.
Relational Operators in R
Operator Description
== Equal to
!= Not equal to
Operation on Vectors
The above mentioned operators work on vectors. The variables used above
were in fact single element vectors.
We can use the function c() (as in concatenate) to make vectors in R.
All operations are carried out in element-wise fashion. Here is an example.
> x <- c(2,8,3)
> y <- c(6,4,1)
> x+y
[1] 8 12 4
> x>y
[1] FALSE TRUE TRUE
When there is a mismatch in length (number of elements) of operand
vectors, the elements in shorter one is recycled in a cyclic manner to match
the length of the longer one.
R Logical Operators
Logical operators are used to carry out Boolean operations like AND, OR
etc.
Logical Operators in R
Operator Description
! Logical NOT
| Element-wise logical OR
|| Logical OR
Assignment Operators in R
Operator Description
Q.1 Basic Operations There are two main different type of interest, simple
and compound. To start let’s create 3 variables, S = 100 (initial investment),
i1=.02 (annual simple interest), i2=.015 (annual compound interest), n=2
(years that the investment will last).
SOL :
S <- 100 i1
<- 0.1 i2 <-
0.09 n <- 2
Q.2 It’s natural to ask which type of interest for this values gives more
amount of money after 2 years (n = 2). Using logical functions <,>, == check
which variable is bigger between simple and compound
SOL :
simple>compound
## [1] TRUE
Q.3 Using logical functions <,>, ==, |, & find out if simple or compound is
equal to 120
Using logical functions <,>, ==, |, & find out if simple and compound is equal
to 120
SOL :
simple>=120|compound>=120
## [1] TRUE
simple>=120&compound>=120
## [1] FALSE
Q.4 Formulas can deal with vectors, so let’s define a vector and use it in one
of the formulas we defined earlier. Let’s define S as a vector with the
following values 100, 96. Remember that c() is the function that allow us to
define vectors.
Apply to S the simple interest formula and store the value of the vector in
simple
SOL :
S <- c(100,95)
simple <- S*(1 + i1*n)
Q.5 Using logical functions <,>, == check if any of the simple values is
smaller or equal to compound
SOL :
simple<=compound
## [1] FALSE TRUE
Q.6 Using the function %/% find out how many $20 candies can you buy
with the money stored in compound
SOL :
compound%/%20
## [1] 5
Q.7 Using the function %% find out how much money is left after buying
the candies.
SOL :
compound%%20
## [1] 18.81
Q.8 Let’s create two new variables, ode defined as rational=1/3 and
decimal=0.33. Using the logical function != Verify if this two values are
different.
SOL :
rational <- 1/3
decimal <- 0.33
rational!=decimal
## [1] TRUE
Q.9 There are other functions that can help us compare two variables.
Use the logical function == verify if rational and decimal are the same.
Use the logical function isTRUE verify if rational and decimal are the same.
Use the logical function identical verify if rational and decimal are the same.
SOL :
rational==decimal
## [1] FALSE
isTRUE(rational==decimal)
## [1] FALSE
identical(rational,decimal)
## [1] FALSE
Q.10 Using the help of the logical functions of the previous exercise find the
approximation that R uses for 1/3. Hint: It is not the value that R prints
when you define 1/3
SOL :
1/3==0.3333333333333333
## [1] TRUE
Exercise- XVI
Logical operations and Factors.
1. Create a logical vector
x <- seq(-3,3,length=200) > 0
2. negate this vector
!x
3. Compute the truth table for logical AND
c(T,T,F,F) & c(T,F,F,T)
4. Explore arithmetic with logical and numeric
1:3 + c(T,F,T)
5. Compute the intersection of {1,2,...,10} and {5,6,...,15}
intersect(1:10,5:15)
6. Create a factor
drinks <- factor(c("beer","beer","wine","water"))
7. Examine the representation of the factor
unclass(drinks)
8. Compute the truth table for logical OR. The function R computes the
logical EXCLUSIVE-OR. What is the difference between the two?
a<- 1:100
a [a%%3==0]
Exercise- XV
Use R to find all the numbers between 1 and n which are multiples of
some m
You will need the operators: and %%
> 1:5
[1] 1 2 3 4 5
> 18 %% 5
[1] 3
> 4 %% 2
[1] 0
1. Use R to find all the numbers between 1 and 2000 which are
multiples of 317.
Sol:
> a <- 1:2000
> a [ a%%317 ==0]
[1] 317 634 951 1268 1585 1902
2. Find all the words with less than 6 or more than 8 characters in the
given vector.
Sol:
> a<- c("Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota",
"Mississippi", "Missouri", "Montana")
> a [nchar(a) >8 | nchar(a)<6]
[1] "Maine" "Massachusetts" "Minnesota" "Mississippi"
3. R has a wide range of built‐in functions.
length(c(4,2,9))
[1] 3
max(c(1,2,4,2,5,-1,1))
[1] 5
sum(c(1,2,3,4))
[1] 10
mean(c(0.5, 3.4, 8.9, 4,4, 6.7))
[1] 4.583333
sd(c(0.5, 3.4, 8.9, 4,4, 6.7))
[1] 2.893729
4. Create a list, and examine elements
x.lis <- list(a=1:10,b=letters[1:3],b=matrix(1:10,ncol=2))
x.lis$1 x.lis[[2]]
5. Element-wise arithmetic with matrices
x.mat <- matrix(1:10,ncol=2)
x.mat+1 x.mat + x.mat
6. Matrix multiplication
x.mat %*% t(x.mat)
7. Compute row and column sums of a matrix
apply (x.mat,1,sum)
apply (x.mat,2,sum)
Exercise- XVI
CONDITIONAL CONTROL STRUCTURES
R if statement
if (test_expression) {
statement
Flowchart of if statement
Example: if statement
x <- 5
print("Positive number")
Output
Output 1
factorial = 1
if(num < 0) {
} else if(num == 0) {
} else {
for(i in 1:num) {
factorial = factorial * i
}
print(paste("The factorial of", num ,"is",factorial))
}
Output
Enter a number: 8
Output 1
"89 is Odd"
Exercise- XVII
ITERATIVE CONTROL STRUCTURES
FOR LOOP
A for loop is used to iterate over a vector in R programming.
statement
Here, sequence is a vector and val takes on each of its value during the loop.
In each iteration, statement is evaluated.
x <- c(2,5,3,9,8,11,6)
count <- 0
for (val in x) {
print(count)
Output
[1] 3
flag = 0
if(num > 1) {
flag = 1
for(i in 2:(num-1)) {
if ((num %% i) == 0) {
flag = 0
break
if(num == 2) flag = 1
if(flag == 1) {
} else {
Output 1
Enter a number: 25
i, '=', num*i))
Output
Enter a number: 7
while (test_expression)
statement
Here, test_expressionis evaluated and the body of the loop is entered if the
result is TRUE.
The statements inside the loop are executed and the flow returns to
evaluate the test_expression again.
i <- 1
while (i < 6) {
print(i)
i = i+1
Output
[1] 1 [1]
2 [1] 3
[1] 4
[1] 5
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while(temp > 0) {
floor(temp / 10)
Output 1
Enter a number: 23
} else {
sum = 0
while(num > 0) {
num = num - 1
Output
Enter a number: 10
1 count = 2
if(nterms <= 0) {
} else {
if(nterms == 1) {
print("Fibonacci sequence:")
print(n1)
} else {
print("Fibonacci sequence:")
print(n1)
print(n2)
nth = n1 + n2
print(nth)
# update values
n1 = n2
n2 = nth
count = count + 1
Output
[1] 0
[1] 1
[1] 1
[1] 2
[1] 3
[1] 5
[1] 8
Exercise- XIX R
Bar Plot
Bar plots can be created in R using the barplot() function.
We can supply a vector or matrix to this function. If we supply a vector, the
plot will have bars with their heights equal to the elements in the vector.
Let us suppose, we have a vector of maximum temperatures (in degree
Celsius) for seven days as follows.
barplot(max.temp)
This function can take a lot of argument to control the way our data is
plotted. You can read about them in the help section ?barplot.
Some of the frequently used ones are, main to give the title, xlab and ylab to
provide labels for the axes, names.arg for naming each bar, col to define
color etc.
We can also plot bars horizontally by providing the argument horiz = TRUE.
ylab = "Day",
col = "darkred",
horiz = TRUE)
Plotting Categorical Data
Sometimes we have to plot the count of each item as bar plots from
categorical data. For example, here is a vector of age of 10 college
freshmen.
Simply doing barplot(age) will not give us the required plot. It will plot 10
bars with height equal to the student’s age. But we want to know the
number of student in each age category.
This count can be quickly found using the table() function, as shown below.
> table(age)
age
16 17 18 19
1 2 6 1
Now plotting this data will give our required bar plot. Note below, that we
define the argument density to shade the bars.
barplot(table(age),
xlab="Age",
ylab="Count",
border="red",
col="blue",
density=10
)
Exercise- XX
R Pie Chart
Pie chart is drawn using the pie() function in R programming . This function
takes in a vector of non-negative numbers.
> expenditure
Now let us draw a simple pie chart out of this data using the pie() function.
pie(expenditure)
We can see above that a pie chart was plotted with 5 slices. The chart was
drawn in anti-clockwise direction using pastel colors.
Example 2: Pie chart with additional parameters
pie(expenditure,
labels=as.character(expenditure),
col=c("red","orange","yellow","blue","green"),
border="brown",
clockwise=TRUE
As seen in the above figure, we have used the actual amount as labels. Also,
the chart is drawn in clockwise fashion.
Since the human eye is relatively bad at judging angles, other types of
charts are appropriate than pie charts.
This is also stated in the R documentation – Pie charts are a very bad way
of displaying information.
Voice Questions and Answers - Cycle-II
x = "AXZ2016"
substr(x,1,3)
44. How to remove leading and trailing spaces .
The trimws() function is used to remove leading and trailing spaces. a = "
David Banes "
trimws(a)
It returns "David Banes".
45. How to generate random numbers between 1 and 100.
x <- seq(-pi,pi,0.1)
plot(x, sin(x))
Adding Titles and Labelling Axes
We can add a title to our plot with the parameter main. Similarly, xlab and
ylab can be used to label the x-axis and y-axis respectively.
plot(x, sin(x),
ylab="sin(x)")
We can see above that the plot is of circular points and black in color. This
is the default color.
We can change the plot type with the argument type. It accepts the
following strings and has the given effect.
"p" - points
"l" - lines
plot(x, sin(x),
ylab="sin(x)",
type="l",
col="blue")
R 3D PLOTS
There are many functions in R programming for creating 3D plots. In this
section, we will discuss on the persp() function which can be used to create
3D surfaces in perspective view.
This function mainly takes in three variables, x, y and z where x and y are
vectors defining the location along x- and y-axis. The height of the surface
(z-axis) will be in the matrix z. As an example,
Let’s plot a cone. A simple right circular cone can be obtained with the
following function.
sqrt(x^2+y^2)
persp(x, y, z)
Adding Titles and Labelling Axes to Plot
Rotational angles
We can define the viewing direction using parameters theta and phi.
By default theta, azimuthal direction, is 0 and phi, colatitude direction, is
15.
Colouring and Shading Plot
Colouring of the plot is done with parameter col.
Similarly, we can add shading with the parameter shade.
persp(x, y, z,
zlab = "Height",