My codes on Book of R

Chapter 2

Numbers, Arithmetic, Assignment and Vectors

Chapter 2 introduces R as a calculator and explores basic arithmetic operations.
Key concepts include:
- Basic arithmetic: addition, subtraction, multiplication, division.
- Exponentiation using ^ and operator precedence (PEMDAS).
- Built-in functions: sqrt(), log(), exp(), abs().
- Creating vectors with c(), sequences with seq(), and repetition with rep().
- Indexing, subsetting, sorting, and modifying vectors.
- R’s handling of very small numbers and scientific notation.

This chapter provides the foundation for working with numbers and vectors in R.

Exercise 2.1

#a
a <- 2.3
(6*a+42)/3^(4.2-3.62)
## [1] 29.50556
#b. Which of the following squares negative 4 and adds 2 to the result?
(-4)^2+2
## [1] 18
-4^2+2
## [1] -14
(-4)^(2+2)
## [1] 256
-4^(2+2)
## [1] -256
#The correct answer is (-4)^2+2
#c.calculate the square root of half of the average of the following numbers#
mean(25.2,15,16.44,15.3,18.6)/2
## [1] 12.6
sqrt(12.6)
## [1] 3.549648
#d. Find loge0.3
log(0.3)
## [1] -1.203973
#e.compute the exponential transform of your answer to (d)
exp(-1.203973)
## [1] 0.2999999
#f.identify the representation of -0.00000000423546322
-0.00000000423546322
## [1] -4.235463e-09

Exercise 2.2

#a.Create an object that stores the value 3^2*4^1/8
obj <- 3^2*4^1/8
#b.overwrite your object in (a) by itself divided by 2.33. Print the result to the console
A <- obj/2.33
A
## [1] 1.93133
#c.Create a new object with the value -8.2*10^-13
B <- -8.2*10^-13
B
## [1] -8.2e-13
#d.Print directly to the console the result of multiplying (b) by (c)
A*B
## [1] -1.583691e-12

Exercise 2.3

#a.Create and store a sequence of values from 5 to -11 that progresses in steps of -0.3
bot <- seq(from=5,to=-11,by=-0.3)
bot
##  [1]   5.0   4.7   4.4   4.1   3.8   3.5   3.2   2.9   2.6   2.3   2.0   1.7
## [13]   1.4   1.1   0.8   0.5   0.2  -0.1  -0.4  -0.7  -1.0  -1.3  -1.6  -1.9
## [25]  -2.2  -2.5  -2.8  -3.1  -3.4  -3.7  -4.0  -4.3  -4.6  -4.9  -5.2  -5.5
## [37]  -5.8  -6.1  -6.4  -6.7  -7.0  -7.3  -7.6  -7.9  -8.2  -8.5  -8.8  -9.1
## [49]  -9.4  -9.7 -10.0 -10.3 -10.6 -10.9
#b.Overwrite the object from (a) using the same sequence with the order reversed
bot <- rev(bot)
bot
##  [1] -10.9 -10.6 -10.3 -10.0  -9.7  -9.4  -9.1  -8.8  -8.5  -8.2  -7.9  -7.6
## [13]  -7.3  -7.0  -6.7  -6.4  -6.1  -5.8  -5.5  -5.2  -4.9  -4.6  -4.3  -4.0
## [25]  -3.7  -3.4  -3.1  -2.8  -2.5  -2.2  -1.9  -1.6  -1.3  -1.0  -0.7  -0.4
## [37]  -0.1   0.2   0.5   0.8   1.1   1.4   1.7   2.0   2.3   2.6   2.9   3.2
## [49]   3.5   3.8   4.1   4.4   4.7   5.0
#c.repeat the vector c(-1,3,-5,7,-9)twice, with each element repeated 10 times,and store the result. Display the result stored from largest to smallest.
vec <-c(-1,3,-5,7,-9)
vec <- rep(vec,times=2,each=10)
vec
##   [1] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1  3  3  3  3  3  3  3  3  3  3 -5 -5 -5 -5 -5
##  [26] -5 -5 -5 -5 -5  7  7  7  7  7  7  7  7  7  7 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
##  [51] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1  3  3  3  3  3  3  3  3  3  3 -5 -5 -5 -5 -5
##  [76] -5 -5 -5 -5 -5  7  7  7  7  7  7  7  7  7  7 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
sort(vec,decreasing =TRUE)
##   [1]  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  3  3  3  3  3
##  [26]  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
##  [51] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
##  [76] -5 -5 -5 -5 -5 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
length(vec)
## [1] 100
#d.Create and store a vector that contains, in any configuration, the following:
seq <- 6:12
rep <- rep(5.3,times=3)
neg <- -3
seq2 <- seq(from=102,to=100,length.out=9)
vec_2 <- c(seq,rep,neg,seq2)
vec_2
##  [1]   6.00   7.00   8.00   9.00  10.00  11.00  12.00   5.30   5.30   5.30
## [11]  -3.00 102.00 101.75 101.50 101.25 101.00 100.75 100.50 100.25 100.00
#e.Confirm that the length of the vector created in (d) is 20
length(vec_2)
## [1] 20

Exercise 2.4

#a.Create and store a vector that contains the following, in this order
seq_3 <- seq(from=3,to=6,length.out=5)
rep_2 <- rep(c(2,-5.1,-33),times=2)
val <- 7/42+2
bar <- c(seq_3,rep_2,val)
bar
##  [1]   3.000000   3.750000   4.500000   5.250000   6.000000   2.000000
##  [7]  -5.100000 -33.000000   2.000000  -5.100000 -33.000000   2.166667
#b.Extract the first and last elements of (a) and store as new object
A <- c(bar[1],bar[length(bar)])
A
## [1] 3.000000 2.166667
#c.Store a third object by omitting first and last elements of a
bam <- bar[-c(1,length(bar))]
bam
##  [1]   3.75   4.50   5.25   6.00   2.00  -5.10 -33.00   2.00  -5.10 -33.00
#d.Reconstruct a using only (b) and (c)
D <- c(A[1],bam,A[2])
D
##  [1]   3.000000   3.750000   4.500000   5.250000   6.000000   2.000000
##  [7]  -5.100000 -33.000000   2.000000  -5.100000 -33.000000   2.166667
#e.Overwrite (a) with sorted values from smallest to largest
bar <- sort(bar,decreasing =FALSE)
bar
##  [1] -33.000000 -33.000000  -5.100000  -5.100000   2.000000   2.000000
##  [7]   2.166667   3.000000   3.750000   4.500000   5.250000   6.000000
#f.Use the colon operator as an index vector to reverse the order of (e)and confirm this is identical to using sort on (e) with decreasing=TRUE.
rev <- (bar[length(bar):1])
rev
##  [1]   6.000000   5.250000   4.500000   3.750000   3.000000   2.166667
##  [7]   2.000000   2.000000  -5.100000  -5.100000 -33.000000 -33.000000
sort(bar,decreasing =TRUE)
##  [1]   6.000000   5.250000   4.500000   3.750000   3.000000   2.166667
##  [7]   2.000000   2.000000  -5.100000  -5.100000 -33.000000 -33.000000
#g.Create a vector from (c) that repeats the third element of (c) three times, the sixth element four times, and the last element once.
foo <- c(rep(bam[3],times=3),rep(bam[6],times=4),bam[length(bam)])
foo
## [1]   5.25   5.25   5.25  -5.10  -5.10  -5.10  -5.10 -33.00
#h. Create a new vector as a copy of (e) by assigning (e) as is to a newly named object. Using this new copy of (e), overwrite the first, the fifth to the seventh (inclusive), and the last element with the values 99 to 95(inclusive),respectively.
bee <- bam
bee
##  [1]   3.75   4.50   5.25   6.00   2.00  -5.10 -33.00   2.00  -5.10 -33.00
bee[c(1,5:7,length(bee))] <- 99:95
bee
##  [1] 99.00  4.50  5.25  6.00 98.00 97.00 96.00  2.00 -5.10 95.00

Exercise 2.5

#a. Convert the vector c(2,0.5,1,2,0.5,1,2,0.5,1) to a vector of only 1s, using a vector of length 3
foo <-  c(2,0.5,1,2,0.5,1,2,0.5,1)
pat <- c(2,0.5,1)
foo2 <- foo/rep(pat,3)
#b
F <- c(45, 77, 20, 19, 101, 120,212)
F
## [1]  45  77  20  19 101 120 212
C <- (5/9)*(F-32)
C
## [1]   7.222222  25.000000  -6.666667  -7.222222  38.333333  48.888889 100.000000
#c.Use the vector c(2,4,6) and the vector c(1,2) in conjunction with rep and * to produce the vector c(2,4,6,4,8,12)
x <- c(2,4,6)
y <- c(1,2)
xy <- (rep(y,each=length(x))*rep(x,times=length(y)))
xy
## [1]  2  4  6  4  8 12
#d. Overwrite the middle four elements of the resulting vector from(c) with the two recycled values-0.1 and-100, in that order
vec <- xy
vec[2:5] <- c(-0.1,-100)
vec
## [1]    2.0   -0.1 -100.0   -0.1 -100.0   12.0

Chapter 3

Matrices and Arrays

Chapter 3 focuses on the concept of assignment and vectors, which are central to working in R. Assignment is the process of giving values to objects, usually done with the arrow <- or the equal sign, and it allows you to store data for later use. R is case-sensitive, meaning object names must be used exactly as defined. Once values are stored in objects, the idea of vectors becomes important. A vector is a collection of elements of the same type, such as numbers, text, or logical values, and it is the most basic data structure in R. Vectors can be created by combining values, generating sequences, or repeating elements, and they can be modified or accessed by referring to their positions. R also allows operations on vectors to be performed element by element, and if vectors are of different lengths, R automatically recycles the shorter one to complete the operation. Built-in functions make it easy to work with vectors by calculating totals, averages, maximums, minimums, and more. Sorting, reversing, and filtering with logical conditions are also part of vector manipulation. When vectors mix different types of data, R coerces them into a single type, usually turning numbers into text if characters are present ### Exercise 3.1

#a. Construct and store a 4x2 matrix that’s filled row-wise with the values 4.3,3.1,8.2,8.2,3.2,0.9,1.6,and6.5,in that order
foo <- matrix(data=c(4.3,3.1,8.2,8.2,3.2,0.9,1.6,6.5),nrow=4,ncol=2,byrow=TRUE)
foo
##      [,1] [,2]
## [1,]  4.3  3.1
## [2,]  8.2  8.2
## [3,]  3.2  0.9
## [4,]  1.6  6.5
#b.Confirm the dimensions of the matrix from (a) are 3x2 if you remove any one row.
b <- foo[-2,]
b
##      [,1] [,2]
## [1,]  4.3  3.1
## [2,]  3.2  0.9
## [3,]  1.6  6.5
dim(b)
## [1] 3 2
#c.Overwrite the second column of the matrix from (a) with that same column sorted from smallest to largest.
foo[,2] <- sort(foo[,2],decreasing = FALSE)
foo
##      [,1] [,2]
## [1,]  4.3  0.9
## [2,]  8.2  3.1
## [3,]  3.2  6.5
## [4,]  1.6  8.2
#d.What does R return if you delete the fourth row and the first column from (c)? Use matrix to ensure the result is a single-column matrix,rather than a vector
bar <- foo[-4,-1]
matrix(data=bar)
##      [,1]
## [1,]  0.9
## [2,]  3.1
## [3,]  6.5
#e.Store the bottom four elements of (c) as a new 2x2 matrix
bee <- matrix(data=c(3.2,1.6,6.5,8.2),nrow=2,ncol=2)
bee
##      [,1] [,2]
## [1,]  3.2  6.5
## [2,]  1.6  8.2

Exercise 3.2

#a. Calculate the following
a <- 2/7
A <- cbind(c(1,2,7),c(2,4,6))
A
##      [,1] [,2]
## [1,]    1    2
## [2,]    2    4
## [3,]    7    6
B <- cbind(c(10,30,50),c(20,40,60))
B
##      [,1] [,2]
## [1,]   10   20
## [2,]   30   40
## [3,]   50   60
a*(A-B)
##            [,1]       [,2]
## [1,]  -2.571429  -5.142857
## [2,]  -8.000000 -10.285714
## [3,] -12.285714 -15.428571
#b. store these two matrices:
A <- cbind(c(1,2,7))
A
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    7
B <- cbind(c(3,4,8))
B
##      [,1]
## [1,]    3
## [2,]    4
## [3,]    8
#Which of the following multiplications are possible? For those that are,compute the result
#b(i) A.B not possible
#b(ii)
t(A)%*%B
##      [,1]
## [1,]   67
#b(iii)
t(B)%*%(A%*%t(A))
##      [,1] [,2] [,3]
## [1,]   67  134  469
#b (iv) gives error
#b(v)
x <- B%*%t(B)
y<- A%*%t(A)
z<- (100*diag(x=3))
z
##      [,1] [,2] [,3]
## [1,]  100    0    0
## [2,]    0  100    0
## [3,]    0    0  100
x+y      
##      [,1] [,2] [,3]
## [1,]   10   14   31
## [2,]   14   20   46
## [3,]   31   46  113
diag(x+y)
## [1]  10  20 113
solve(x+y-z)
##              [,1]         [,2]        [,3]
## [1,] -0.007923676  0.003123274 0.007843334
## [2,]  0.003123274 -0.005350239 0.011483806
## [3,]  0.007843334  0.011483806 0.017584735
#c
A <- diag(c(2,3,5,-1))
A
##      [,1] [,2] [,3] [,4]
## [1,]    2    0    0    0
## [2,]    0    3    0    0
## [3,]    0    0    5    0
## [4,]    0    0    0   -1
# confirm that A^1.A-I4 provides a 4x4 matrix of zeros
dim(c)
## NULL
solve(A)%*%A-diag(x=4)
##      [,1] [,2] [,3] [,4]
## [1,]    0    0    0    0
## [2,]    0    0    0    0
## [3,]    0    0    0    0
## [4,]    0    0    0    0

Exercise 3.3

#a.Create and store a three-dimensional array with six layers of a 4x2 matrix,filled with a decreasing sequence of values between 4.8 and 0.1 of the appropriate length
foo <- seq(from=4.8,to=0.1,length.out=48)
foo
##  [1] 4.8 4.7 4.6 4.5 4.4 4.3 4.2 4.1 4.0 3.9 3.8 3.7 3.6 3.5 3.4 3.3 3.2 3.1 3.0
## [20] 2.9 2.8 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0 1.9 1.8 1.7 1.6 1.5 1.4 1.3 1.2 1.1
## [39] 1.0 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1
foo <- rev(foo)
foo
##  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
## [20] 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8
## [39] 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8
bar <- array(foo,dim = c(4,2,6))
bar
## , , 1
## 
##      [,1] [,2]
## [1,]  0.1  0.5
## [2,]  0.2  0.6
## [3,]  0.3  0.7
## [4,]  0.4  0.8
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  0.9  1.3
## [2,]  1.0  1.4
## [3,]  1.1  1.5
## [4,]  1.2  1.6
## 
## , , 3
## 
##      [,1] [,2]
## [1,]  1.7  2.1
## [2,]  1.8  2.2
## [3,]  1.9  2.3
## [4,]  2.0  2.4
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  2.5  2.9
## [2,]  2.6  3.0
## [3,]  2.7  3.1
## [4,]  2.8  3.2
## 
## , , 5
## 
##      [,1] [,2]
## [1,]  3.3  3.7
## [2,]  3.4  3.8
## [3,]  3.5  3.9
## [4,]  3.6  4.0
## 
## , , 6
## 
##      [,1] [,2]
## [1,]  4.1  4.5
## [2,]  4.2  4.6
## [3,]  4.3  4.7
## [4,]  4.4  4.8
#b.Extract and store as a new object the fourth- and first-row elements, in that order, of the second column only of all layers of (a)
B <-bar[c(4,1),2,]
B
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]  0.8  1.6  2.4  3.2  4.0  4.8
## [2,]  0.5  1.3  2.1  2.9  3.7  4.5
#c.Use a fourfold repetition of the second row of the matrix formed in (b) to fill a new array of dimensions 2x2x2x3.
new <- array(rep(B[2,],times=4),dim=c(2,2,2,3))
new
## , , 1, 1
## 
##      [,1] [,2]
## [1,]  0.5  2.1
## [2,]  1.3  2.9
## 
## , , 2, 1
## 
##      [,1] [,2]
## [1,]  3.7  0.5
## [2,]  4.5  1.3
## 
## , , 1, 2
## 
##      [,1] [,2]
## [1,]  2.1  3.7
## [2,]  2.9  4.5
## 
## , , 2, 2
## 
##      [,1] [,2]
## [1,]  0.5  2.1
## [2,]  1.3  2.9
## 
## , , 1, 3
## 
##      [,1] [,2]
## [1,]  3.7  0.5
## [2,]  4.5  1.3
## 
## , , 2, 3
## 
##      [,1] [,2]
## [1,]  2.1  3.7
## [2,]  2.9  4.5
#d.Create a new array comprised of the results of deleting the sixth layer of (a)
new_2 <- bar[,,-6]
new_2
## , , 1
## 
##      [,1] [,2]
## [1,]  0.1  0.5
## [2,]  0.2  0.6
## [3,]  0.3  0.7
## [4,]  0.4  0.8
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  0.9  1.3
## [2,]  1.0  1.4
## [3,]  1.1  1.5
## [4,]  1.2  1.6
## 
## , , 3
## 
##      [,1] [,2]
## [1,]  1.7  2.1
## [2,]  1.8  2.2
## [3,]  1.9  2.3
## [4,]  2.0  2.4
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  2.5  2.9
## [2,]  2.6  3.0
## [3,]  2.7  3.1
## [4,]  2.8  3.2
## 
## , , 5
## 
##      [,1] [,2]
## [1,]  3.3  3.7
## [2,]  3.4  3.8
## [3,]  3.5  3.9
## [4,]  3.6  4.0
#e.Overwrite the second and fourth row elements of the second column of layers 1, 3, and 5 of (d) with 99.
new_2[c(2,4),2,c(1,3,5)] <- -99
new_2
## , , 1
## 
##      [,1]  [,2]
## [1,]  0.1   0.5
## [2,]  0.2 -99.0
## [3,]  0.3   0.7
## [4,]  0.4 -99.0
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  0.9  1.3
## [2,]  1.0  1.4
## [3,]  1.1  1.5
## [4,]  1.2  1.6
## 
## , , 3
## 
##      [,1]  [,2]
## [1,]  1.7   2.1
## [2,]  1.8 -99.0
## [3,]  1.9   2.3
## [4,]  2.0 -99.0
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  2.5  2.9
## [2,]  2.6  3.0
## [3,]  2.7  3.1
## [4,]  2.8  3.2
## 
## , , 5
## 
##      [,1]  [,2]
## [1,]  3.3   3.7
## [2,]  3.4 -99.0
## [3,]  3.5   3.9
## [4,]  3.6 -99.0

Chapter 4

Non-numeric Values

Chapter 4 introduces matrices and arrays as extensions of vectors into higher dimensions. A matrix is a two-dimensional structure of rows and columns containing elements of the same type. Matrices can be created with functions like matrix(), or by combining vectors with rbind() and cbind(). They can be subset by specifying row and column indices, or by extracting entire rows and columns. Operations such as addition, subtraction, multiplication, transposition, and inversion follow linear algebra rules. Arrays generalize matrices into three or more dimensions, storing elements across multiple layers. Both matrices and arrays are internally stored as vectors with dimension attributes. This means vectorized operations still apply but extend across rows, columns, or higher dimensions. These structures are essential for handling complex, structured datasets in R. ### Exercise 4.1

#a. Store the following vector of 15 values as an object in your workspace: c(6,9,7,3,6,7,9,6,3,6,6,7,1,9,1)
bop <- c(6,9,7,3,6,7,9,6,3,6,6,7,1,9,1)
#a.i.Those equal to 6
bop==6
##  [1]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE
## [13] FALSE FALSE FALSE
#a ii.Those greater than or equal to 6
bop>=6
##  [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
## [13] FALSE  TRUE FALSE
#a.iii. Those less than 6+2
bop<6+2
##  [1]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
## [13]  TRUE FALSE  TRUE
#a.iv.Those not equal to 6
bop!=6
##  [1] FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE
## [13]  TRUE  TRUE  TRUE
#b.Create a new vector from the one used in (a) by deleting its first three elements. With this new vector, fill a 2x2x3 array
bop[-c(1:3)]
##  [1] 3 6 7 9 6 3 6 6 7 1 9 1
mow <- array(bop[-c(1:3)],dim = c(2,2,3))
mow
## , , 1
## 
##      [,1] [,2]
## [1,]    3    7
## [2,]    6    9
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    6    6
## [2,]    3    6
## 
## , , 3
## 
##      [,1] [,2]
## [1,]    7    9
## [2,]    1    1
#Examine the array for the following entries:
#b.i.Those less than or equal to 6 divided by 2, plus 4
B <- mow<=6/2+4
B
## , , 1
## 
##      [,1]  [,2]
## [1,] TRUE  TRUE
## [2,] TRUE FALSE
## 
## , , 2
## 
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## 
## , , 3
## 
##      [,1]  [,2]
## [1,] TRUE FALSE
## [2,] TRUE  TRUE
#b.ii.Those less than or equal to 6 divided by 2, plus 4, after increasing every element in the array by 2
C <- mow+2<=6/2+4
C
## , , 1
## 
##       [,1]  [,2]
## [1,]  TRUE FALSE
## [2,] FALSE FALSE
## 
## , , 2
## 
##       [,1]  [,2]
## [1,] FALSE FALSE
## [2,]  TRUE FALSE
## 
## , , 3
## 
##       [,1]  [,2]
## [1,] FALSE FALSE
## [2,]  TRUE  TRUE
#c.Confirm the specific locations of elements equal to 0 in the 10x10 identity matrix I10
vee <- diag(x=10)
vee
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]    1    0    0    0    0    0    0    0    0     0
##  [2,]    0    1    0    0    0    0    0    0    0     0
##  [3,]    0    0    1    0    0    0    0    0    0     0
##  [4,]    0    0    0    1    0    0    0    0    0     0
##  [5,]    0    0    0    0    1    0    0    0    0     0
##  [6,]    0    0    0    0    0    1    0    0    0     0
##  [7,]    0    0    0    0    0    0    1    0    0     0
##  [8,]    0    0    0    0    0    0    0    1    0     0
##  [9,]    0    0    0    0    0    0    0    0    1     0
## [10,]    0    0    0    0    0    0    0    0    0     1
vee==0
##        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
##  [1,] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [2,]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [3,]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [4,]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [5,]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [6,]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
##  [7,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
##  [8,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
##  [9,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
## [10,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
#d.Check whether any of the values of the logical arrays created in(b) are TRUE. If they are, check whether they are all TRUE
any(B)
## [1] TRUE
all(B)
## [1] FALSE
any(C)
## [1] TRUE
all(C)
## [1] FALSE
#e.By extracting the diagonal elements of the logical matrix created in (c), use any to confirm there are no TRUE entries.
boa <- diag(vee)
boa
##  [1] 1 1 1 1 1 1 1 1 1 1
any(boa)
## Warning in any(boa): coercing argument of type 'double' to logical
## [1] TRUE

Exercise 4.2

#a.Store the vector c(7,1,7,10,5,9,10,3,10,8) as foo. Identify the elements greater than 5 OR equal to 2.
foo <-c(7,1,7,10,5,9,10,3,10,8)
foo2 <- foo>5|foo==2
foo2
##  [1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
#b.Store the vector c(8,8,4,4,5,1,5,6,6,8) as bar. Identify the elements less than or equal to 6 AND not equal to 4
bar <- c(8,8,4,4,5,1,5,6,6,8)
bar2 <- bar<=6&bar!=4
bar2
##  [1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
#c. Identify the elements that satisfy (a) in foo AND satisfy (b) in bar
foo2&bar2
##  [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE
#d.Store a third vector called baz that is equal to the element-wise sum of foo and bar
baz <- c(foo+bar)
baz
##  [1] 15  9 11 14 10 10 15  9 16 16
#d.i.The elements of baz greater than or equal to 14 but not equal to 15
baz>=14&baz!=15
##  [1] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE
#d.ii.The elements of the vector obtained via an element-wise division of baz by foo that are greater than 4 OR less than or equal to 2
bay <- baz/foo
bay
##  [1] 2.142857 9.000000 1.571429 1.400000 2.000000 1.111111 1.500000 3.000000
##  [9] 1.600000 2.000000
bay>4|bay<=2
##  [1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
#e. Confirm that using the long version in all of the preceding exercises performs only the first comparison (that is, the results each match the first entries of the previously obtained vectors)

Exercise 4.3

#a.Store this vector of 10 values: foo <- c(7,5,6,1,2,10,8,3,8,2).Then, do the following:
foo <- c(7,5,6,1,2,10,8,3,8,2)
#a.i.Extract the elements greater than or equal to 5, storing the result as bar
bar <- foo[foo>=5]
bar
## [1]  7  5  6 10  8  8
#a.ii.Display the vector containing those elements from foo that remain after omitting all elements that are greater than or equal to 5
vec <- foo[foo<5]
vec
## [1] 1 2 3 2
#b.Use bar from (a)(i) to construct a 2 3 matrix called baz, filled in a row-wise fashion.
baz <- matrix(data=bar,nrow=2,ncol=3,byrow=TRUE)
baz
##      [,1] [,2] [,3]
## [1,]    7    5    6
## [2,]   10    8    8
#b.i Replace any elements that are equal to 8 with the squared value of the element in row 1, column 2 of baz itself
baz[baz==8] <- baz[1,2]^2
baz
##      [,1] [,2] [,3]
## [1,]    7    5    6
## [2,]   10   25   25
#b.ii Confirm that all values in baz are now less than or equal to 25 AND greater than 4
all(baz<=255&baz>4)
## [1] TRUE
#c.
qux <- array(c(10, 5, 1, 4, 7, 4, 3, 3, 1, 3, 4, 3, 1, 7, 8, 3, 7, 3),dim=c(3,2,3))
qux
## , , 1
## 
##      [,1] [,2]
## [1,]   10    4
## [2,]    5    7
## [3,]    1    4
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    3    3
## [2,]    3    4
## [3,]    1    3
## 
## , , 3
## 
##      [,1] [,2]
## [1,]    1    3
## [2,]    7    7
## [3,]    8    3
#c.i. Identify the dimension-specific index positions of elements that are either 3 OR 4.
which(qux==3|qux==4,arr.ind = T)
##       dim1 dim2 dim3
##  [1,]    1    2    1
##  [2,]    3    2    1
##  [3,]    1    1    2
##  [4,]    2    1    2
##  [5,]    1    2    2
##  [6,]    2    2    2
##  [7,]    3    2    2
##  [8,]    1    2    3
##  [9,]    3    2    3
#c. ii.Replace all elements in qux that are less than 3 OR greater than or equal to 7 with the value 100.
qux[qux<3|qux>=7] <- 100
qux
## , , 1
## 
##      [,1] [,2]
## [1,]  100    4
## [2,]    5  100
## [3,]  100    4
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    3    3
## [2,]    3    4
## [3,]  100    3
## 
## , , 3
## 
##      [,1] [,2]
## [1,]  100    3
## [2,]  100  100
## [3,]  100    3
#d.Return to foo from (a). Use the vector c(F,T) to extract every second value from foo
foo[c(F,T)]
## [1] NA NA NA NA NA NA NA  7
foo[c(0,1)]
## [1] 7

Chapter 5

Lists and Data Frames

Chapter 5 introduces non-numeric values, expanding beyond numbers to characters, factors, and logicals. Character data represent text and can be created with quotes, concatenated with paste(), and analyzed with string functions. Factors are categorical variables that store fixed levels, making them vital for statistical modeling and grouping data. Logical values (TRUE and FALSE) are used for condition checks, filtering, and control flow. They often result from comparison operators such as ==, !=, <, and >. Logical vectors can index subsets, allowing selective data extraction. The chapter also shows how characters, factors, and logicals interact with vectors and functions. Coercion between types is possible, for example, converting logicals to numeric (1s and 0s). These non-numeric values enrich R’s data handling beyond raw numbers. ### Exercise 5.1

#a.Create a list that contains, in this order, a sequence of 20 evenly spaced numbers between 4 and 4; a 3 3 matrix of the logical vector c(F,T,T,T,F,T,T,F,F) filled column-wise;
#A character vector with the two strings "don" and "quixote"; and a factor vector containing the observations c("LOW","MED","LOW","MED","MED","HIGH"). Then, do the following:
foo <- list (seq(from=-4,to=4,length.out=20),matrix(data=c(FALSE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,FALSE),nrow=3,ncol=3), c("don","quixote"),factor(c("LOW","MED","LOW","MED","MED","HIGH")))
foo
## [[1]]
##  [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
##  [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
## [13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
## [19]  3.5789474  4.0000000
## 
## [[2]]
##       [,1]  [,2]  [,3]
## [1,] FALSE  TRUE  TRUE
## [2,]  TRUE FALSE FALSE
## [3,]  TRUE  TRUE FALSE
## 
## [[3]]
## [1] "don"     "quixote"
## 
## [[4]]
## [1] LOW  MED  LOW  MED  MED  HIGH
## Levels: HIGH LOW MED
#a.i.Extract row elements 2 and 1 of columns 2 and 3, in that order, of the logical matrix.
lag <- foo[[2]][c(2, 1), c(2, 3)]
lag
##       [,1]  [,2]
## [1,] FALSE FALSE
## [2,]  TRUE  TRUE
#a.ii. Use sub to overwrite "quixote" with "Quixote" and "don" with "Don" inside the list. Then, using the newly overwritten list member, concatenate to the console screen the following statement exactly:"Windmills! ATTACK!"-\Don Quixote/
foo[[3]] <- sub("don", "Don", foo[[3]])
foo[[3]] <- sub("quixote", "Quixote", foo[[3]])
foo
## [[1]]
##  [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
##  [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
## [13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
## [19]  3.5789474  4.0000000
## 
## [[2]]
##       [,1]  [,2]  [,3]
## [1,] FALSE  TRUE  TRUE
## [2,]  TRUE FALSE FALSE
## [3,]  TRUE  TRUE FALSE
## 
## [[3]]
## [1] "Don"     "Quixote"
## 
## [[4]]
## [1] LOW  MED  LOW  MED  MED  HIGH
## Levels: HIGH LOW MED
cat("\"Windmills!  ATTACK!\"\n\t-\\Don Quixote/-\n")
## "Windmills!  ATTACK!"
##  -\Don Quixote/-
#a.iii. Obtain all values from the sequence between 4 and 4 that are greater than 1.
bar <- seq(from=-4,to=4,length.out=20)
bar
##  [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
##  [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
## [13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
## [19]  3.5789474  4.0000000
bar>1
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#a.iv. Using which, determine which indexes in the factor vector are assigned the "MED" level.
bee <- factor(c("LOW","MED","LOW","MED","MED","HIGH"))
bee
## [1] LOW  MED  LOW  MED  MED  HIGH
## Levels: HIGH LOW MED
which(x=bee=="MED")
## [1] 2 4 5
#b.Create a new list with the factor vector from (a) as a component named "facs";
#the numeric vector c(3,2.1,3.3,4,1.5,4.9) as a component named "nums";
#and a nested list comprised of the first three members of the list from (a) (use list slicing to obtain this),named "oldlist"
kid <- list(facs= c("LOW","MED","LOW","MED","MED","HIGH"),nums= c(3,2.1,3.3,4,1.5,4.9),oldlist=foo[1:3])
kid
## $facs
## [1] "LOW"  "MED"  "LOW"  "MED"  "MED"  "HIGH"
## 
## $nums
## [1] 3.0 2.1 3.3 4.0 1.5 4.9
## 
## $oldlist
## $oldlist[[1]]
##  [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
##  [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
## [13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
## [19]  3.5789474  4.0000000
## 
## $oldlist[[2]]
##       [,1]  [,2]  [,3]
## [1,] FALSE  TRUE  TRUE
## [2,]  TRUE FALSE FALSE
## [3,]  TRUE  TRUE FALSE
## 
## $oldlist[[3]]
## [1] "Don"     "Quixote"
#b.i.Extract the elements of "facs" that correspond to elements of "nums" that are greater than or equal to 3
kid$facs[kid$nums>=3]
## [1] "LOW"  "LOW"  "MED"  "HIGH"
#b.ii.Add a new member to the list named "flags". This member should be a logical vector of length 6, obtained as a twofold repetition of the third column of the logical matrix in the"oldlist" component
sad <- kid$oldlist[[2]][,3]
sad
## [1]  TRUE FALSE FALSE
flags <- rep(sad,times=2)
flags
## [1]  TRUE FALSE FALSE  TRUE FALSE FALSE
kid$flags <- flags
kid
## $facs
## [1] "LOW"  "MED"  "LOW"  "MED"  "MED"  "HIGH"
## 
## $nums
## [1] 3.0 2.1 3.3 4.0 1.5 4.9
## 
## $oldlist
## $oldlist[[1]]
##  [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
##  [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
## [13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
## [19]  3.5789474  4.0000000
## 
## $oldlist[[2]]
##       [,1]  [,2]  [,3]
## [1,] FALSE  TRUE  TRUE
## [2,]  TRUE FALSE FALSE
## [3,]  TRUE  TRUE FALSE
## 
## $oldlist[[3]]
## [1] "Don"     "Quixote"
## 
## 
## $flags
## [1]  TRUE FALSE FALSE  TRUE FALSE FALSE
#b.iii.Use "flags" and the logical negation operator ! to extract the entries of "num" corresponding to FALSE
kid$nums[!kid$flags]
## [1] 2.1 3.3 1.5 4.9
#b.iv. Overwrite the character string vector component of "oldlist" with the single character string "Don Quixote"
kid$oldlist[[3]] <- "Don Quixote"
kid
## $facs
## [1] "LOW"  "MED"  "LOW"  "MED"  "MED"  "HIGH"
## 
## $nums
## [1] 3.0 2.1 3.3 4.0 1.5 4.9
## 
## $oldlist
## $oldlist[[1]]
##  [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
##  [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263  0.2105263  0.6315789
## [13]  1.0526316  1.4736842  1.8947368  2.3157895  2.7368421  3.1578947
## [19]  3.5789474  4.0000000
## 
## $oldlist[[2]]
##       [,1]  [,2]  [,3]
## [1,] FALSE  TRUE  TRUE
## [2,]  TRUE FALSE FALSE
## [3,]  TRUE  TRUE FALSE
## 
## $oldlist[[3]]
## [1] "Don Quixote"
## 
## 
## $flags
## [1]  TRUE FALSE FALSE  TRUE FALSE FALSE

Exercise 5.2

#a.
dframe <- data.frame(person=c("Stan","Francine","Steve","Roger","Hayley","Klaus"),sex=c("M","F","M","M","F","M"),funny=c("High","Med","Low","High","Med","Med"))
dframe
##     person sex funny
## 1     Stan   M  High
## 2 Francine   F   Med
## 3    Steve   M   Low
## 4    Roger   M  High
## 5   Hayley   F   Med
## 6    Klaus   M   Med
#b.
age <- c("41","41","15","1600","21","60")
dframe <- cbind(dframe,age)
dframe
##     person sex funny  age
## 1     Stan   M  High   41
## 2 Francine   F   Med   41
## 3    Steve   M   Low   15
## 4    Roger   M  High 1600
## 5   Hayley   F   Med   21
## 6    Klaus   M   Med   60
#c.
dframe <- dframe[, c(1, 4, 2, 3)]
dframe
##     person  age sex funny
## 1     Stan   41   M  High
## 2 Francine   41   F   Med
## 3    Steve   15   M   Low
## 4    Roger 1600   M  High
## 5   Hayley   21   F   Med
## 6    Klaus   60   M   Med
#d.
mydata<-data.frame(person=c("Peter","Lois","Meg","Chris","Stewie"),
                   age=c(42,40,17,14,1),
                   sex=factor(c("M","F","F","M","M")),
                   stringsAsFactors=FALSE)
mydata
##   person age sex
## 1  Peter  42   M
## 2   Lois  40   F
## 3    Meg  17   F
## 4  Chris  14   M
## 5 Stewie   1   M
newrecord <- data.frame(person="Brian",age=7,
                        sex=factor("M",levels=levels(mydata$sex)))
newrecord
##   person age sex
## 1  Brian   7   M
mydata <- rbind(mydata,newrecord)
mydata
##   person age sex
## 1  Peter  42   M
## 2   Lois  40   F
## 3    Meg  17   F
## 4  Chris  14   M
## 5 Stewie   1   M
## 6  Brian   7   M
funny<-c("High","High","Low","Med","High","Med")
funny<-factor(x=funny,levels=c("Low","Med","High"))
funny
## [1] High High Low  Med  High Med 
## Levels: Low Med High
mydata<-cbind(mydata,funny)
mydata$age.mon<-mydata$age*12
mydata
##   person age sex funny age.mon
## 1  Peter  42   M  High     504
## 2   Lois  40   F  High     480
## 3    Meg  17   F   Low     204
## 4  Chris  14   M   Med     168
## 5 Stewie   1   M  High      12
## 6  Brian   7   M   Med      84
mydata2 <- mydata[,-5]
mydata2
##   person age sex funny
## 1  Peter  42   M  High
## 2   Lois  40   F  High
## 3    Meg  17   F   Low
## 4  Chris  14   M   Med
## 5 Stewie   1   M  High
## 6  Brian   7   M   Med
#e.
mydataframe <- rbind(dframe,mydata2)
mydataframe
##      person  age sex funny
## 1      Stan   41   M  High
## 2  Francine   41   F   Med
## 3     Steve   15   M   Low
## 4     Roger 1600   M  High
## 5    Hayley   21   F   Med
## 6     Klaus   60   M   Med
## 7     Peter   42   M  High
## 8      Lois   40   F  High
## 9       Meg   17   F   Low
## 10    Chris   14   M   Med
## 11   Stewie    1   M  High
## 12    Brian    7   M   Med
#f.
mydataframe[mydataframe$sex=="F"& mydataframe$funny==c("Med","High"),c("person","age")]
##   person age
## 5 Hayley  21
## 8   Lois  40
#g
mydataframe[substr(x=mydataframe$person,start = 1,stop = 1)=="S",]
##    person age sex funny
## 1    Stan  41   M  High
## 3   Steve  15   M   Low
## 11 Stewie   1   M  High

Chapter 6

Special values ,Classes and Coercion

Chapter 6 covers lists and data frames, two powerful structures for organizing complex data. A list is a flexible container that can hold elements of different types, including vectors, matrices, and even other lists. Lists are created with list() and elements can be accessed using double brackets [[ ]] or the $ operator for named items. They are especially useful for grouping related but heterogeneous objects together. Data frames, by contrast, are tabular structures where each column is a vector of the same length. They resemble spreadsheets, with rows representing observations and columns representing variables. Functions like data.frame(), head(), and str() allow creation and inspection of data frames. Indexing in data frames can be done by row and column numbers, or by variable names. Lists and data frames are central for statistical modeling, as they align with real-world datasets. ### Exercise 6.1

#a. Store the following vector
foo <- c(13563,-14156,-14319,16981,12921,11979,9568,8833,-12968,8133)
foo
##  [1]  13563 -14156 -14319  16981  12921  11979   9568   8833 -12968   8133
#a.i. Output all elements of foo that, when raised to a power of 75,are NOT infinite
foo[is.finite(foo^75)]
## [1] 11979  9568  8833  8133
#a.ii. Return the elements of foo, excluding those that result in negative infinity when raised to a power of 75
foo[foo^75 != -Inf]
## [1] 13563 16981 12921 11979  9568  8833  8133
#b. Store the following 3 4 matrix as the object bar:
bar <- matrix(data=c(7787540,2755145,2376430,-3647888,-3546625,-7333385,3659969,-7058569,-3980381,5597634,7669482,4703200),nrow=3,ncol=4,byrow=T)
bar
##          [,1]     [,2]    [,3]     [,4]
## [1,]  7787540  2755145 2376430 -3647888
## [2,] -3546625 -7333385 3659969 -7058569
## [3,] -3980381  5597634 7669482  4703200
#b i. Identify the coordinate-specific indexes of the entries of bar that are NaN when you raise bar to a power of 65 and divide by infinity.
which(is.nan(bar^65 / Inf), arr.ind = TRUE)
##       row col
##  [1,]   1   1
##  [2,]   2   1
##  [3,]   3   1
##  [4,]   1   2
##  [5,]   2   2
##  [6,]   3   2
##  [7,]   1   3
##  [8,]   2   3
##  [9,]   3   3
## [10,]   1   4
## [11,]   2   4
## [12,]   3   4
#b.ii.  Return the values in bar that are NOT NaN when bar is raised to a power of 67 and infinity is added to the result. Confirm this is identical to identifying those values in bar that, when raised to a power of 67, are not equal to negative infinity
not_NaN <- bar[!is.nan(bar^67 + Inf)]
not_NaN
## [1] 7787540 2755145 5597634 2376430 3659969 7669482 4703200
not_negInf <- bar[bar^67 != -Inf]
not_negInf
## [1] 7787540 2755145 5597634 2376430 3659969 7669482 4703200
identical(not_NaN, not_negInf)
## [1] TRUE
#b.iii. Identify those values in bar that are either negative infinity OR finite when you raise bar to a power of 67.
bar[(bar^67 == -Inf) | is.finite(bar^67)]
## [1] -3546625 -3980381 -7333385 -3647888 -7058569

Exerise 6.2

#a. Consider the following line of code
foo <- c(4.3,2.2,NULL,2.4,NaN,3.3,3.1,NULL,3.4,NA)
foo
## [1] 4.3 2.2 2.4 NaN 3.3 3.1 3.4  NA
#a.i. The length of foo is 8
length(foo==8)
## [1] 8
#a.ii. Calling which(x=is.na(x=foo)) will not result in 4 and 8
which(x=is.na(x=foo))
## [1] 4 8
#a.iii. Checking is.null(x=foo) will provide you with the locations of the two NULL values that are present
is.null(x=foo)
## [1] FALSE
#a.iv.Executing is.na(x=foo[8])+4/NULL will not result in NA.
is.na(x=foo[8])+4/NULL
## numeric(0)
#b.  Create and store a list containing a single member: the vector c(7,7,NA,3,NA,1,1,5,NA). Then, do the following:
bar <- c(7, 7, NA, 3, NA, 1, 1, 5, NA)
bar
## [1]  7  7 NA  3 NA  1  1  5 NA
#b.i. Name the member "alpha"
barlist <- list("alpha"=bar)
barlist
## $alpha
## [1]  7  7 NA  3 NA  1  1  5 NA
#b.ii. Confirm that the list doesn’t have a member with the name "beta" using the appropriate logical valued function.
barlist$beta
## NULL
#b.iii. Add a new member called beta, which is the vector obtained by identifying the index positions of alpha that are NA
Na <- which(is.na(bar))
Na
## [1] 3 5 9
barlist$beta <- Na
barlist
## $alpha
## [1]  7  7 NA  3 NA  1  1  5 NA
## 
## $beta
## [1] 3 5 9

Chapter 7

Basic Plotting

Chapter 7 introduces the basics of plotting in R using its base graphics system. The primary tool is the plot() function, which creates scatterplots from numeric vectors. Different plot types can be specified with the type argument, such as points, lines, or both. Customization is possible through titles, axis labels, and scaling options. Colors, line types, and point shapes can be adjusted to improve readability and aesthetics. Additional elements like text(), abline(), points(), and lines() allow annotation and layering. Legends can be added with legend() to clarify meaning in complex plots. R’s graphics parameters also allow fine-tuning of margins, labels, and layout. These features make it possible to transform raw data into clear visual stories. Overall, the chapter builds a foundation for effective data visualization in R. ### Exercise 7.1

x <- c(-3,-2,-1,0,1,2,3)
y <- c(7,8,9,10,11,12,13)
plot(x,y,type="n",main="")
abline(h=c(7,13),v=c(-3,3),col=8,lty=2,lwd=2)
arrows(x0=-2.5,y0=12.5,x1=-1,y1=10.5)
arrows(x0=-2.5,y0=10,x1=-1,y1=10)
arrows(x0=-2.5,y0=7.5,x1=-1,y1=9.5)
arrows(x0=2.5,y0=12.5,x1=1,y1=10.5)
arrows(x0=2.5,y0=10,x1=1,y1=10)
arrows(x0=2.5,y0=7.5,x1=1,y1=9.5)
text(x=0,y=10,labels=("SOMETHING\nPROFOUND"))

#b.
Weight <- c(55,85,75,42,93,63,58,75,89,67)
Height <- c(161,185,174,154,188,178,170,167,181,178)
Sex <- c("female","male","male","female","male","male","female","male","male","female")
plot(Weight,Height,type="n",main="IBK's Plot",xlab = "Weight",ylab = "Height")
points(Weight[Sex=="male"],Height[Sex=="male"],col="blue",pch=15)
points(Weight[Sex=="female"],Height[Sex=="female"],col="magenta",pch=15)
legend("bottomright",
       legend=c("male","female"),
       col=c("blue","magenta"),pch=c(15,15))

Exercise 7.2

library(ggplot2)
df <- data.frame(Weight=c(55,85,75,42,93,63,58,75,89,67),Height=c(161,185,174,154,188,178,170,167,181,178),Sex=c("female","male","male","female","male","male","female","male","male","female"))
ggplot(df,aes(x=Weight,y=Height,color=Sex,shape=Sex)) + geom_point(size=3) + labs(title = "IBK's Plot",x="Weight(kg)",y="Height(cm)") + scale_color_manual(values = c("male"="blue", "female"="magenta")) +theme_minimal()

Functions Learned So Far

Here is a summary of the R functions I have used so far and what they are useful for:

Function Purpose Example
c() Combine values into a vector c(1,2,3)
seq() Generate sequences of numbers seq(1,10,by=2)
rep() Repeat values a set number of times rep(5, times=3)
length() Count number of elements in a vector length(c(1,2,3,4))
sort() Sort values in ascending/descending order sort(c(4,1,3))
rev() Reverse the order of elements rev(c(1,2,3))
mean() Calculate the average mean(c(2,4,6))
sqrt() Compute square root sqrt(16)
log() Compute logarithms (default base e) log(10)
exp() Exponential function \(e^x\) exp(2)
sum() Add all elements sum(c(1,2,3))
prod() Multiply all elements prod(c(1,2,3))
matrix() Create a matrix matrix(1:6, nrow=2)
rbind() Bind by rows rbind(c(1,2), c(3,4))
cbind() Bind by columns cbind(c(1,2), c(3,4))
solve() Solve linear systems / matrix inverse solve(matrix(c(2,1,1,2),2,2))
is.na() Check for missing values is.na(c(1,NA,3))
any() Test if any values are TRUE any(c(TRUE,FALSE))
all() Test if all values are TRUE all(c(TRUE,TRUE))
plot() Create a base plot plot(1:5, 6:10)
points() Add more points to a plot points(3,7,col="red")
lines() Add lines to a plot lines(1:5, 6:10)
title() Add/modify a title title("My Plot")
legend() Add a legend legend("topleft","Data",pch=1)
abline() Add horizontal/vertical/regression lines abline(h=5,col="blue")
text() Add text to a plot text(2,8,"Hello")